DateTimeHelper.cs 4.6 KB

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