足力健后端,使用.netcore版本,合并1个项目使用

DateTimeConvert.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System;
  2. namespace Utility
  3. {
  4. public class DateTimeConvert
  5. {
  6. private static readonly DateTime StartedDateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  7. /// <summary>
  8. /// 根据阿拉伯数字返回月份的名称(可更改为某种语言)
  9. /// </summary>
  10. public static string[] Monthes
  11. {
  12. get
  13. {
  14. return new[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
  15. }
  16. }
  17. /// <summary>
  18. /// 判断是不是当月的最后一天,是返回true,否返回flase
  19. /// </summary>
  20. /// <param name="Dt"></param>
  21. /// <returns></returns>
  22. public bool IsLastMonthDay(DateTime Dt)
  23. {
  24. bool Flag = false;
  25. DateTime MonthClearTime = Dt.AddDays(1 - Dt.Day).AddMonths(1).AddDays(-1);
  26. if (DateDiff("D", Dt, MonthClearTime) == 0)
  27. {
  28. Flag = true;
  29. }
  30. return Flag;
  31. }
  32. #region 日期时间方法
  33. /// <summary>
  34. /// 日期时间相减方法 M 月,D 天,H小时,T分,S秒
  35. /// </summary>
  36. /// <param name="dtype"></param>
  37. /// <param name="D1"></param>
  38. /// <param name="D2"></param>
  39. /// <returns></returns>
  40. public static int DateDiff(string dtype, DateTime D1, DateTime D2)
  41. {
  42. int d = 0;
  43. int h = 0;
  44. int t = 0;
  45. switch (dtype.ToUpper())
  46. {
  47. case "M":
  48. d = D2.Year - D1.Year;
  49. h = D2.Month - D1.Month;
  50. if (d > 0)
  51. {
  52. return d * 12 + h + 1;
  53. }
  54. if (d == 0)
  55. {
  56. if (h > 0)
  57. {
  58. return h + 1;
  59. }
  60. if (h == 0)
  61. {
  62. return 1;
  63. }
  64. return d * 12 + h - 1;
  65. }
  66. return d * 12 + h - 1;
  67. case "D":
  68. TimeSpan Num = Convert.ToDateTime(D2.ToShortDateString()) - Convert.ToDateTime(D1.ToShortDateString());
  69. return Num.Days;
  70. case "H":
  71. d = DateDiff("D", D1, D2);
  72. h = D2.Hour - D1.Hour;
  73. return h + (d * 24);
  74. case "T":
  75. d = Convert.ToInt32(D2.Subtract(D1).Days);
  76. h = Convert.ToInt32(D2.Subtract(D1).Hours);
  77. t = Convert.ToInt32(D2.Subtract(D1).Minutes);
  78. return ((h + (d * 24)) * 60) + t;
  79. case "S":
  80. d = Convert.ToInt32(D2.Subtract(D1).Days);
  81. h = Convert.ToInt32(D2.Subtract(D1).Hours);
  82. t = Convert.ToInt32(D2.Subtract(D1).Minutes);
  83. return ((((h + (d * 24)) * 60) + t) * 60) + Convert.ToInt32(D2.Subtract(D1).Seconds);
  84. default:
  85. return -1;
  86. }
  87. }
  88. public static long DateDiff(DateInterval Interval, DateTime StartDate, DateTime EndDate)
  89. {
  90. long lngDateDiffValue = 0;
  91. var TS = new TimeSpan(EndDate.Ticks - StartDate.Ticks);
  92. switch (Interval)
  93. {
  94. case DateInterval.Second:
  95. lngDateDiffValue = (long)TS.TotalSeconds;
  96. break;
  97. case DateInterval.Minute:
  98. lngDateDiffValue = (long)TS.TotalMinutes;
  99. break;
  100. case DateInterval.Hour:
  101. lngDateDiffValue = (long)TS.TotalHours;
  102. break;
  103. case DateInterval.Day:
  104. lngDateDiffValue = TS.Days;
  105. break;
  106. case DateInterval.Week:
  107. lngDateDiffValue = TS.Days / 7;
  108. break;
  109. case DateInterval.Month:
  110. lngDateDiffValue = TS.Days / 30;
  111. break;
  112. case DateInterval.Quarter:
  113. lngDateDiffValue = (TS.Days / 30) / 3;
  114. break;
  115. case DateInterval.Year:
  116. lngDateDiffValue = TS.Days / 365;
  117. break;
  118. }
  119. return (lngDateDiffValue);
  120. }
  121. #endregion
  122. /// <summary>
  123. /// 将时间戳转换为DateTime类型
  124. /// </summary>
  125. /// <param name="timetag"></param>
  126. /// <param name="toLocalTime"></param>
  127. /// <returns></returns>
  128. public static DateTime FromTimetag(long timetag, bool toLocalTime = true)
  129. {
  130. var time = new DateTime(StartedDateTime.Ticks + timetag * 10000);
  131. return toLocalTime ? time.ToLocalTime() : time;
  132. }
  133. /// <summary>
  134. /// 将Datetime类型转换为时间戳
  135. /// </summary>
  136. /// <param name="dateTime"></param>
  137. /// <returns></returns>
  138. public static long ToTimetag(DateTime dateTime)
  139. {
  140. return (dateTime.ToUniversalTime().Ticks - StartedDateTime.Ticks) / 10000;
  141. }
  142. ///<summary>
  143. ///由秒数得到日期几天几小时。。。
  144. ///</summary
  145. ///<param name="t">秒数</param>
  146. ///<param name="type">0:转换后带秒,1:转换后不带秒</param>
  147. ///<returns>几天几小时几分几秒</returns>
  148. public static string parseTimeSeconds(int t, int type)
  149. {
  150. string r = "";
  151. int day, hour, minute, second;
  152. if (t >= 86400) //天,
  153. {
  154. day = Convert.ToInt32(t / 86400);
  155. hour = Convert.ToInt32((t % 86400) / 3600);
  156. minute = Convert.ToInt32((t % 86400 % 3600) / 60);
  157. second = Convert.ToInt32(t % 86400 % 3600 % 60);
  158. if (type == 0)
  159. r = day + ("天") + hour + ("小时") + minute + ("分") + second + ("秒");
  160. else
  161. r = day + ("天") + hour + ("小时") + minute + ("分");
  162. }
  163. else if (t >= 3600)//时,
  164. {
  165. hour = Convert.ToInt32(t / 3600);
  166. minute = Convert.ToInt32((t % 3600) / 60);
  167. second = Convert.ToInt32(t % 3600 % 60);
  168. if (type == 0)
  169. r = hour + ("小时") + minute + ("分") + second + ("秒");
  170. else
  171. r = hour + ("小时") + minute + ("分");
  172. }
  173. else if (t >= 60)//分
  174. {
  175. minute = Convert.ToInt32(t / 60);
  176. second = Convert.ToInt32(t % 60);
  177. r = minute + ("分") + second + ("秒");
  178. }
  179. else
  180. {
  181. second = Convert.ToInt32(t);
  182. r = second + ("秒");
  183. }
  184. return r;
  185. }
  186. /// <summary>
  187. /// 当前日期时间,转换为秒
  188. /// </summary>
  189. /// <returns>秒数</returns>
  190. public static string xDateSeconds()
  191. {
  192. long xdateseconds = 0;
  193. DateTime xdatenow = DateTime.UtcNow; //当前UTC时间
  194. long xminute = 60; //一分种60秒
  195. long xhour = 3600;
  196. long xday = 86400;
  197. long byear = 1970;//从1970-1-1 0:00:00开始到现在所过的秒
  198. long[] xmonth = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  199. long[] xyear = { 365, 366 };
  200. long num = 0;
  201. xdateseconds += xdatenow.Second; //算秒
  202. xdateseconds += xdatenow.Minute * xminute; //算分
  203. xdateseconds += xdatenow.Hour * xhour; //算时
  204. xdateseconds += (xdatenow.Day - 1) * xday; //算天
  205. //算月(月换成天算)
  206. if (DateTime.IsLeapYear(xdatenow.Year))
  207. {
  208. xdateseconds += (xmonth[xdatenow.Month - 1] + 1) * xday;
  209. }
  210. else
  211. {
  212. xdateseconds += (xmonth[xdatenow.Month - 1]) * xday;
  213. }
  214. //算年(年换成天算)
  215. long lyear = xdatenow.Year - byear;
  216. for (int i = 0; i < lyear; i++)
  217. {
  218. if (DateTime.IsLeapYear((int)byear + i))
  219. {
  220. num++;
  221. }
  222. }
  223. xdateseconds += ((lyear - num) * xyear[0] + num * xyear[1]) * xday;
  224. return xdateseconds.ToString();
  225. }
  226. /// <summary>
  227. /// 秒数转日期
  228. /// </summary>
  229. /// <param name="Value">秒数</param>
  230. /// <returns>日期</returns>
  231. public static DateTime GetGMTDateTime(int Value)
  232. {
  233. //秒数转时间日期
  234. //GMT时间从2000年1月1日开始,先把它作为赋为初值
  235. long Year = 2000, Month = 1, Day = 1;
  236. long Hour = 0, Min = 0, Sec = 0;
  237. //临时变量
  238. long iYear = 0, iDay = 0;
  239. long iHour = 0, iMin = 0, iSec = 0;
  240. //计算文件创建的年份
  241. iYear = Value / (365 * 24 * 60 * 60);
  242. Year = Year + iYear;
  243. //计算文件除创建整年份以外还有多少天
  244. iDay = (Value % (365 * 24 * 60 * 60)) / (24 * 60 * 60);
  245. //把闰年的年份数计算出来
  246. int RInt = 0;
  247. for (int i = 2000; i < Year; i++)
  248. {
  249. if ((i % 4) == 0)
  250. RInt = RInt + 1;
  251. }
  252. //计算文件创建的时间(几时)
  253. iHour = ((Value % (365 * 24 * 60 * 60)) % (24 * 60 * 60)) / (60 * 60);
  254. Hour = Hour + iHour;
  255. //计算文件创建的时间(几分)
  256. iMin = (((Value % (365 * 24 * 60 * 60)) % (24 * 60 * 60)) % (60 * 60)) / 60;
  257. Min = Min + iMin;
  258. //计算文件创建的时间(几秒)
  259. iSec = (((Value % (365 * 24 * 60 * 60)) % (24 * 60 * 60)) % (60 * 60)) % 60;
  260. Sec = Sec + iSec;
  261. DateTime t = new DateTime((int)Year, (int)Month, (int)Day, (int)Hour, (int)Min, (int)Sec);
  262. DateTime t1 = t.AddDays(iDay - RInt);
  263. return t1;
  264. }
  265. }
  266. }