C#中时间戳和日期相互转换
1、日期转换时间戳
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary> /// 将日期转换为时间戳 /// </summary> /// <param name="time"></param> /// <returns></returns> private long ConverDateTimeToLong(DateTime time) { DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0)); long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位 return t; }
2、时间戳转换日期
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary> /// 将时间戳转换为日期 /// </summary> /// <param name="timespan"></param> /// <returns></returns> private DateTime ConvertToDateTime(long timespan) { DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); string l = timespan + "0000"; TimeSpan ts = new TimeSpan(long.Parse(l)); return dtStart.Add(ts); }
posted on 2020-08-06 16:52 lovezj9012 阅读(673) 评论(0) 编辑 收藏 举报