1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace LYFZ.WinAPI
- {
- /// <summary>
- /// 时间戳转换
- /// </summary>
- public class TimeStampConversion
- {
- public TimeStampConversion()
- {
- }
- /// <summary>
- /// 时间戳开始时间
- /// </summary>
- static DateTime TimeStampStartTime
- {
- get
- {
- return TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
- }
- }
- /// <summary>
- /// DateTime转换为JavaScript时间戳 13位数长度
- /// </summary>
- /// <param name="t"></param>
- /// <returns></returns>
- public static long DateTimeToJavaScriptTimeStamp(DateTime t)
- {
- return (long)(t - TimeStampConversion.TimeStampStartTime).TotalMilliseconds; //相差毫秒数
- }
- /// <summary>
- /// JavaScript时间戳转换为DateTime
- /// </summary>
- /// <param name="t"></param>
- /// <returns></returns>
- public static System.DateTime JavaScriptTimeStampToDateTime(long timeStamp)
- {
- return TimeStampConversion.TimeStampStartTime.AddMilliseconds(timeStamp);
- }
- /// <summary>
- /// DateTime转换为Unix时间戳 10位数长度
- /// </summary>
- /// <param name="timeStamp"></param>
- /// <returns></returns>
- public static long DateTimeToUnixTimeStamp(DateTime t)
- {
- return (long)(t - TimeStampConversion.TimeStampStartTime).TotalSeconds; // 相差秒数
- }
- /// <summary>
- /// Unix时间戳转换为DateTime
- /// </summary>
- /// <param name="timeStamp"></param>
- /// <returns></returns>
- public static System.DateTime UnixTimeStampToDateTime(long timeStamp)
- {
- return TimeStampConversion.TimeStampStartTime.AddSeconds(timeStamp);
- }
- }
- }
|