简要
本篇文章主要介绍基于.net(c#)封装的时间帮助类。主要功能是将时间戳与DateTime类型相互转换。
1.其中包括将毫秒级时间戳(13位)、秒级时间戳(10位)转换为DateTime。
2.获取当前日期所在周的周一、周五时间。比如:今天是2018-11-8 周四 ,那么这周的周一2018-11-5则周五为2018-11-9。
讨论Q群:580749909
代码
public class DateHelper { private static DateTime _startTime; private static DateTime _now; private static DateTime _friday; private static DateTime _monday; public static DateTime Now { get { if (_now == default(DateTime)) { _now = DateTime.Now; } return _now; } } public static DateTime StartTime { get { if (_startTime == default(DateTime)) { _startTime = new DateTime(Now.Year, Now.Month, Now.Day, 9, 30, 00); } return _startTime; } } /// <summary> /// 获取当前时间所在周的星期五 /// </summary> public static DateTime Friday { get { if (_friday == default(DateTime)) { _friday = GetFriday(); } return _friday; } } /// <summary> /// 获取当前时间所在周的星期一 /// </summary> public static DateTime Monday { get { if (_monday == default(DateTime)) { _monday = GetMonday(); } return _monday; } } /// <summary> /// 计算本周起始日期(礼拜一的日期) /// </summary> /// <param name="someDate">该周中任意一天</param> /// <returns>返回礼拜一日期,后面的具体时、分、秒和传入值相等</returns> private static DateTime GetMonday() { DateTime someDate = Now; int i = Now.DayOfWeek - DayOfWeek.Monday; if (i == -1) i = 6;// i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Monday=-1,必须+7=6。 TimeSpan ts = new TimeSpan(i, 0, 0, 0); return someDate.Subtract(ts); } /// <summary> /// 计算本周结束日期(礼拜五的日期) /// </summary> /// <param name="someDate">该周中任意一天</param> /// <returns>返回礼拜日日期,后面的具体时、分、秒和传入值相等</returns> private static DateTime GetFriday() { DateTime someDate = Now; int i = someDate.DayOfWeek - DayOfWeek.Friday; if (i != 0) i = Math.Abs(i);// 因为枚举原因,Sunday排在最前,此时差值要取绝对值。 TimeSpan ts = new TimeSpan(i, 0, 0, 0); return someDate.Add(ts); } /// <summary> /// 毫秒级时间戳(长度13位) /// </summary> /// <param name="dateTime"></param> /// <returns>int32</returns> public static int ToJSTimeStamp32(DateTime dateTime) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 int timeStamp = (int)(dateTime - startTime).TotalMilliseconds; // 相差毫秒数 return timeStamp; } /// <summary> /// 毫秒级时间戳(长度13位) /// </summary> /// <param name="dateTime"></param> /// <returns>int64</returns> public static long ToJSTimeStamp64(DateTime dateTime) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 long timeStamp = (long)(dateTime - startTime).TotalMilliseconds; // 相差毫秒数 return timeStamp; } /// <summary> /// 秒级时间戳(长度10位) /// </summary> /// <param name="dateTime"></param> /// <returns>int32</returns> public static int ToUnixTimeStamp32(DateTime dateTime) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 int timeStamp = (int)(dateTime - startTime).TotalSeconds; // 相差秒数 return timeStamp; } /// <summary> /// 秒级时间戳(长度10位) /// </summary> /// <param name="dateTime"></param> /// <returns>int64</returns> public static long ToUnixTimeStamp64(DateTime dateTime) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 long timeStamp = (long)(dateTime - startTime).TotalSeconds; // 相差秒数 return timeStamp; } /// <summary> /// 将毫秒级时间戳(13)转换为时间 /// </summary> /// <param name="unixTimeStamp"></param> /// <returns></returns> public static DateTime ToJSDateTime(long jsTimeStamp) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 DateTime dt = startTime.AddMilliseconds(jsTimeStamp); return dt; } /// <summary> /// 将秒级时间戳(10位)转换为时间 /// </summary> /// <param name="unixTimeStamp"></param> /// <returns></returns> public static DateTime ToUnixDateTime(long unixTimeStamp) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 DateTime dt = startTime.AddSeconds(unixTimeStamp); return dt; } }