鄂尔多斯-招源科技

DateTimeConvert.cs 10KB

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