1、根据时间戳获取时间(10位/13位)
/// <summary> /// 根据时间戳获取时间(10位) /// </summary> public static DateTime TimeStampToDateTime(string timeStamp) { Int64 begtime = Convert.ToInt64(timeStamp) * 10000000; DateTime dt_1970 = new DateTime(1970, 1, 1, 8, 0, 0); long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度 long time_tricks = tricks_1970 + begtime;//日志日期刻度 DateTime dt = new DateTime(time_tricks);//转化为DateTime return dt; } /// <summary> /// 根据时间戳获取时间(13位) /// </summary> public static DateTime TimeLongStampToDateTime(string timeStamp) { Int64 begtime = Convert.ToInt64(timeStamp) * 10000; DateTime dt_1970 = new DateTime(1970, 1, 1, 8, 0, 0); long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度 long time_tricks = tricks_1970 + begtime;//日志日期刻度 DateTime dt = new DateTime(time_tricks);//转化为DateTime return dt; }
2、根据时间获取时间戳(10位/13位)
/// <summary> /// 获取时间戳 /// </summary> /// <param name="time">时间格式字符串(yyyy-MM-dd HH:mm:ss)</param> /// <param name="type">0:10位,1:13位</param> /// <returns></returns> public static string Timestamps(string time, int type) { string stamps = string.Empty; if (!string.IsNullOrEmpty(time)) { stamps = type == 0 ? ((Convert.ToDateTime(time).ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString() : ((Convert.ToDateTime(time).ToUniversalTime().Ticks - 621355968000000000) / 10000).ToString(); } else { if (type == 0) { long lstamps = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;//10位时间戳生成方式 stamps = lstamps.ToString(); } else { long lstamps = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000;//13位时间戳生成方式 stamps = lstamps.ToString(); } } return stamps; }
标签:转换,string,C#,tricks,1970,时间,time,dt,DateTime From: https://www.cnblogs.com/zyg316/p/18525047