TimeHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace IoTIntegrationPlatform.Common
  8. {
  9. /// <summary>
  10. /// 时间公共类
  11. /// </summary>
  12. public static class TimeHelper
  13. {
  14. /// <summary>
  15. /// 相差秒
  16. /// </summary>
  17. /// <param name="startTime"></param>
  18. /// <param name="endTime"></param>
  19. /// <returns></returns>
  20. public static double DiffSeconds(DateTime startTime, DateTime endTime)
  21. {
  22. TimeSpan secondSpan = new TimeSpan(endTime.Ticks - startTime.Ticks);
  23. return secondSpan.TotalSeconds;
  24. }
  25. /// <summary>
  26. /// 相差分钟数
  27. /// </summary>
  28. /// <param name="startTime"></param>
  29. /// <param name="endTime"></param>
  30. /// <returns></returns>
  31. public static double DiffMinutes(DateTime startTime, DateTime endTime)
  32. {
  33. TimeSpan minuteSpan = new TimeSpan(endTime.Ticks - startTime.Ticks);
  34. return minuteSpan.TotalMinutes;
  35. }
  36. /// <summary>
  37. /// 相差小时
  38. /// </summary>
  39. /// <param name="startTime"></param>
  40. /// <param name="endTime"></param>
  41. /// <returns></returns>
  42. public static double DiffHours(DateTime startTime, DateTime endTime)
  43. {
  44. TimeSpan hoursSpan = new TimeSpan(endTime.Ticks - startTime.Ticks);
  45. return hoursSpan.TotalHours;
  46. }
  47. /// <summary>
  48. /// 相差天数
  49. /// </summary>
  50. /// <param name="startTime"></param>
  51. /// <param name="endTime"></param>
  52. /// <returns></returns>
  53. public static double DiffDays(DateTime startTime, DateTime endTime)
  54. {
  55. TimeSpan daysSpan = new TimeSpan(endTime.Ticks - startTime.Ticks);
  56. return daysSpan.TotalDays;
  57. }
  58. /// <summary>
  59. /// 相差月数
  60. /// </summary>
  61. /// <param name="startTime"></param>
  62. /// <param name="endTime"></param>
  63. /// <returns></returns>
  64. public static double DiffMonths(DateTime startTime, DateTime endTime)
  65. {
  66. int months = (endTime.Year - startTime.Year) * 12 + endTime.Month - startTime.Month;
  67. if (startTime.Day > endTime.Day) months--; // 如果第一个日期的日大于第二个日期的日,则减去一个月
  68. return months;
  69. }
  70. /// <summary>
  71. /// 获取当前时间戳
  72. /// </summary>
  73. /// <returns></returns>
  74. public static long GetCurrentTimestamp()
  75. {
  76. DateTimeOffset currentTime = DateTimeOffset.Now;
  77. long timestamp = currentTime.ToUnixTimeSeconds();
  78. return timestamp;
  79. }
  80. /// <summary>
  81. /// 获取当前时间戳[毫秒]
  82. /// </summary>
  83. /// <param name="dateTime"></param>
  84. /// <returns></returns>
  85. public static long GetCurrentTimestampTicks()
  86. {
  87. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  88. long t = Convert.ToInt64(ts.TotalMilliseconds);
  89. return t;
  90. }
  91. /// <summary>
  92. /// 时间转时间戳[毫秒]
  93. /// </summary>
  94. /// <param name="date"></param>
  95. /// <returns></returns>
  96. public static long DateToTimestampTicks(DateTime date)
  97. {
  98. DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));//当地时区
  99. TimeSpan ts = date - startTime;
  100. long timestamp = Convert.ToInt64(ts.TotalSeconds);
  101. return timestamp;
  102. }
  103. /// <summary>
  104. /// 某天的开始
  105. /// </summary>
  106. /// <param name="datetime"></param>
  107. /// <returns></returns>
  108. public static DateTime DayOfZeroSeconds(DateTime datetime)
  109. {
  110. return datetime.AddDays(1 - datetime.Day).Date;
  111. }
  112. /// <summary>
  113. /// 某天的最后一秒
  114. /// </summary>
  115. /// <param name="datetime"></param>
  116. /// <returns></returns>
  117. public static DateTime DayOfLastSeconds(DateTime datetime)
  118. {
  119. return datetime.AddDays(1).AddSeconds(-1);
  120. }
  121. }
  122. }