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