DateTimeUtility.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Ant.Service.Utility
  7. {
  8. public static class DateTimeUtility
  9. {
  10. // 时间戳转为C#格式时间
  11. public static DateTime StampToDateTime(this string timeStamp)
  12. {
  13. DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToUniversalTime(new DateTime(1970, 1, 1));
  14. long lTime = long.Parse(timeStamp + "0000000");
  15. TimeSpan toNow = new TimeSpan(lTime);
  16. return dateTimeStart.Add(toNow);
  17. }
  18. // DateTime时间格式转换为Unix时间戳格式
  19. public static int UnixDateTimeToStamp(this System.DateTime time)
  20. {
  21. var now = TimeZone.CurrentTimeZone.ToUniversalTime(time);
  22. System.DateTime startTime = TimeZone.CurrentTimeZone.ToUniversalTime(new System.DateTime(1970, 1, 1));
  23. return (int)(now - startTime).TotalSeconds;
  24. }
  25. /// <summary>
  26. /// DateTime时间格式转换为Unix时间戳格式
  27. /// </summary>
  28. /// <param name="time"> DateTime时间格式</param>
  29. /// <returns>Unix时间戳格式</returns>
  30. public static int ConvertDateTimeInt(System.DateTime time)
  31. {
  32. System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
  33. return (int)(time - startTime).TotalSeconds;
  34. }
  35. public static DateTime StampToDate(string timeStamp)
  36. {
  37. DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  38. long lTime = long.Parse(timeStamp + "0000000");
  39. TimeSpan toNow = new TimeSpan(lTime);
  40. return dateTimeStart.Add(toNow);
  41. }
  42. /// <summary>
  43. ///
  44. /// </summary>
  45. /// <param name="time"></param>
  46. /// <returns></returns>
  47. public static int DateTimeToStamp(this System.DateTime time)
  48. {
  49. var now = TimeZone.CurrentTimeZone.ToLocalTime(time);
  50. System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
  51. return (int)(now - startTime).TotalSeconds;
  52. }
  53. public static UInt32 DateTimeToUInt32(this System.DateTime time)
  54. {
  55. var now = TimeZone.CurrentTimeZone.ToUniversalTime(time);
  56. System.DateTime startTime = TimeZone.CurrentTimeZone.ToUniversalTime(new System.DateTime(1970, 1, 1));
  57. return (UInt32)(now - startTime).TotalSeconds;
  58. }
  59. /// <summary>
  60. /// 秒转换小时
  61. /// </summary>
  62. /// <param name="time"></param>
  63. /// <returns></returns>
  64. public static string SecondToHour(double time)
  65. {
  66. string str = "";
  67. int hour = 0;
  68. int minute = 0;
  69. int second = 0;
  70. second = Convert.ToInt32(time);
  71. if (second > 60)
  72. {
  73. minute = second / 60;
  74. second = second % 60;
  75. }
  76. if (minute > 60)
  77. {
  78. hour = minute / 60;
  79. minute = minute % 60;
  80. }
  81. return (hour + "小时" + minute + "分钟"
  82. + second + "秒");
  83. }
  84. /// <summary>
  85. /// 两个时间之间差(可以计算到几天几时几分几秒)
  86. /// </summary>
  87. /// <param name="date1"></param>
  88. /// <param name="date2"></param>
  89. /// <returns></returns>
  90. public static string DateTimeDifference(DateTime date1, DateTime date2)
  91. {
  92. DateTime d1 = date1;
  93. DateTime d2 = date2;
  94. TimeSpan d3 = d2.Subtract(d1);
  95. StringBuilder sbstr = new StringBuilder();
  96. if (d3.Days > 0)
  97. {
  98. sbstr.AppendFormat("{0}天", d3.Days);
  99. }
  100. if (d3.Hours > 0)
  101. {
  102. sbstr.AppendFormat("{0}小时", d3.Hours);
  103. }
  104. if (d3.Minutes > 0)
  105. {
  106. sbstr.AppendFormat("{0}分钟", d3.Minutes);
  107. }
  108. if (d3.Seconds > 0)
  109. {
  110. sbstr.AppendFormat("{0}秒", d3.Seconds);
  111. }
  112. return sbstr.ToString();
  113. }
  114. /// <summary>
  115. /// 秒转换小时
  116. /// </summary>
  117. /// <param name="time"></param>
  118. /// <returns></returns>
  119. public static string MinuteToHour(double time)
  120. {
  121. string str = "";
  122. int hour = 0;
  123. int minute = 0;
  124. int second = 0;
  125. minute = Convert.ToInt32(time);
  126. if (minute > 60)
  127. {
  128. hour = minute / 60;
  129. minute = minute % 60;
  130. }
  131. return (hour + "小时" + minute + "分钟");
  132. }
  133. /// <summary>
  134. /// 把分钟数转化成几小时几分钟(100 -> 01:40)
  135. /// </summary>
  136. /// <param name="minutes">待转化的分钟数</param>
  137. /// <returns>几小时几分钟</returns>
  138. public static string ConvertToTime(double minutes)
  139. {
  140. int M = 0;//待转化的分钟数;
  141. int h = 0;//小时
  142. int m = 0;//分钟
  143. h = M / 60;
  144. m = M % 60;
  145. DateTime time = DateTime.Parse("00:00");
  146. time = time.AddHours(h).AddMinutes(m);
  147. string R = time.ToString("HH:mm");
  148. if (R == "00:00")
  149. {
  150. R = ""; ;
  151. }
  152. return R;
  153. }
  154. }
  155. }