日期工具类
1 public class DateTimeHelper 2 { 3 /// <summary> 4 /// 5 /// </summary> 6 /// <param name="dateTime"></param> 7 /// <returns></returns> 8 public static DateTime GetBeginTime(DateTime? dateTime, int days = 0) 9 { 10 if (dateTime == DateTime.MinValue || dateTime == null) 11 { 12 return DateTime.Now.AddDays(days); 13 } 14 return dateTime ?? DateTime.Now; 15 } 16 #region 时间戳转换 17 18 /// <summary> 19 /// 时间戳转本地时间-时间戳精确到秒 20 /// </summary> 21 public static DateTime ToLocalTimeDateBySeconds(long unix) 22 { 23 var dto = DateTimeOffset.FromUnixTimeSeconds(unix); 24 return dto.ToLocalTime().DateTime; 25 } 26 27 /// <summary> 28 /// 时间转时间戳Unix-时间戳精确到秒 29 /// </summary> 30 public static long ToUnixTimestampBySeconds(DateTime dt) 31 { 32 DateTimeOffset dto = new DateTimeOffset(dt); 33 return dto.ToUnixTimeSeconds(); 34 } 35 36 /// <summary> 37 /// 时间戳转本地时间-时间戳精确到毫秒 38 /// </summary> 39 public static DateTime ToLocalTimeDateByMilliseconds(long unix) 40 { 41 var dto = DateTimeOffset.FromUnixTimeMilliseconds(unix); 42 return dto.ToLocalTime().DateTime; 43 } 44 45 /// <summary> 46 /// 时间转时间戳Unix-时间戳精确到毫秒 47 /// </summary> 48 public static long ToUnixTimestampByMilliseconds(DateTime dt) 49 { 50 DateTimeOffset dto = new DateTimeOffset(dt); 51 return dto.ToUnixTimeMilliseconds(); 52 } 53 54 #endregion 55 56 #region 毫秒转天时分秒 57 /// <summary> 58 /// 毫秒转天时分秒 59 /// </summary> 60 /// <param name="ms"></param> 61 /// <returns></returns> 62 public static string FormatTime(long ms) 63 { 64 int ss = 1000; 65 int mi = ss * 60; 66 int hh = mi * 60; 67 int dd = hh * 24; 68 69 long day = ms / dd; 70 long hour = (ms - day * dd) / hh; 71 long minute = (ms - day * dd - hour * hh) / mi; 72 long second = (ms - day * dd - hour * hh - minute * mi) / ss; 73 long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss; 74 75 string sDay = day < 10 ? "0" + day : "" + day; //天 76 string sHour = hour < 10 ? "0" + hour : "" + hour;//小时 77 string sMinute = minute < 10 ? "0" + minute : "" + minute;//分钟 78 string sSecond = second < 10 ? "0" + second : "" + second;//秒 79 string sMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;//毫秒 80 sMilliSecond = milliSecond < 100 ? "0" + sMilliSecond : "" + sMilliSecond; 81 82 return string.Format("{0} 天 {1} 小时 {2} 分 {3} 秒", sDay, sHour, sMinute, sSecond); 83 } 84 #endregion 85 86 #region 获取unix时间戳 87 /// <summary> 88 /// 获取unix时间戳(毫秒) 89 /// </summary> 90 /// <param name="dt"></param> 91 /// <returns></returns> 92 public static long GetUnixTimeStamp(DateTime dt) 93 { 94 long unixTime = ((DateTimeOffset)dt).ToUnixTimeMilliseconds(); 95 return unixTime; 96 } 97 98 public static long GetUnixTimeSeconds(DateTime dt) 99 { 100 long unixTime = ((DateTimeOffset)dt).ToUnixTimeSeconds(); 101 return unixTime; 102 } 103 #endregion 104 105 #region 获取日期天的最小时间 106 public static DateTime GetDayMinDate(DateTime dt) 107 { 108 DateTime min = new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0); 109 return min; 110 } 111 #endregion 112 113 #region 获取日期天的最大时间 114 public static DateTime GetDayMaxDate(DateTime dt) 115 { 116 DateTime max = new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59); 117 return max; 118 } 119 #endregion 120 121 #region 获取日期天的最大时间 122 public static string FormatDateTime(DateTime? dt) 123 { 124 if (dt != null) 125 { 126 if (dt.Value.Year == DateTime.Now.Year) 127 { 128 return dt.Value.ToString("MM-dd HH:mm"); 129 } 130 else 131 { 132 return dt.Value.ToString("yyyy-MM-dd HH:mm"); 133 } 134 } 135 return string.Empty; 136 } 137 #endregion
public static double GetTimestamp(DateTime d)
{
TimeSpan ts = d - new DateTime(1970, 1, 1);
return ts.TotalMilliseconds;
} 138 }