人民医院API

DateTimeConvert.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using System;
  2. namespace RMYY_CallCenter_Api.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. public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
  37. {
  38. string dateDiff = null;
  39. TimeSpan ts = DateTime1.Subtract(DateTime2).Duration();
  40. string days = "";string hours = "";
  41. if (ts.Days.ToString() != "0")
  42. days = ts.Days.ToString() + "天";
  43. if(ts.Hours.ToString() !="0")
  44. hours = ts.Hours.ToString() + "时";
  45. dateDiff = days + hours + ts.Minutes.ToString() + "分"; // + ts.Seconds.ToString() + "秒";
  46. return dateDiff;
  47. }
  48. /// <summary>
  49. /// 日期时间相减方法 M 月,D 天,H小时,T分,S秒
  50. /// </summary>
  51. /// <param name="dtype"></param>
  52. /// <param name="D1"></param>
  53. /// <param name="D2"></param>
  54. /// <returns></returns>
  55. public static int DateDiff(string dtype, DateTime D1, DateTime D2)
  56. {
  57. int d = 0;
  58. int h = 0;
  59. int t = 0;
  60. switch (dtype.ToUpper())
  61. {
  62. case "M":
  63. d = D2.Year - D1.Year;
  64. h = D2.Month - D1.Month;
  65. if (d > 0)
  66. {
  67. return d * 12 + h + 1;
  68. }
  69. if (d == 0)
  70. {
  71. if (h > 0)
  72. {
  73. return h + 1;
  74. }
  75. if (h == 0)
  76. {
  77. return 1;
  78. }
  79. return d * 12 + h - 1;
  80. }
  81. return d * 12 + h - 1;
  82. case "D":
  83. TimeSpan Num = Convert.ToDateTime(D2.ToShortDateString()) - Convert.ToDateTime(D1.ToShortDateString());
  84. return Num.Days;
  85. case "H":
  86. d = DateDiff("D", D1, D2);
  87. h = D2.Hour - D1.Hour;
  88. return h + (d * 24);
  89. case "T":
  90. d = Convert.ToInt32(D2.Subtract(D1).Days);
  91. h = Convert.ToInt32(D2.Subtract(D1).Hours);
  92. t = Convert.ToInt32(D2.Subtract(D1).Minutes);
  93. return ((h + (d * 24)) * 60) + t;
  94. case "S":
  95. d = Convert.ToInt32(D2.Subtract(D1).Days);
  96. h = Convert.ToInt32(D2.Subtract(D1).Hours);
  97. t = Convert.ToInt32(D2.Subtract(D1).Minutes);
  98. return ((((h + (d * 24)) * 60) + t) * 60) + Convert.ToInt32(D2.Subtract(D1).Seconds);
  99. default:
  100. return -1;
  101. }
  102. }
  103. #endregion
  104. /// <summary>
  105. /// 将时间戳转换为DateTime类型
  106. /// </summary>
  107. /// <param name="timetag"></param>
  108. /// <param name="toLocalTime"></param>
  109. /// <returns></returns>
  110. public static DateTime FromTimetag(long timetag,bool toLocalTime = true)
  111. {
  112. var time = new DateTime(StartedDateTime.Ticks + timetag*10000);
  113. return toLocalTime ? time.ToLocalTime() : time;
  114. }
  115. /// <summary>
  116. /// 将Datetime类型转换为时间戳
  117. /// </summary>
  118. /// <param name="dateTime"></param>
  119. /// <returns></returns>
  120. public static long ToTimetag(DateTime dateTime)
  121. {
  122. return (dateTime.ToUniversalTime().Ticks - StartedDateTime.Ticks)/10000;
  123. }
  124. ///<summary>
  125. ///由秒数得到日期几天几小时。。。
  126. ///</summary
  127. ///<param name="t">秒数</param>
  128. ///<param name="type">0:转换后带秒,1:转换后不带秒</param>
  129. ///<returns>几天几小时几分几秒</returns>
  130. public static string parseTimeSeconds(int t, int type)
  131. {
  132. string r = "";
  133. int day, hour, minute, second;
  134. if (t >= 86400) //天,
  135. {
  136. day = Convert.ToInt32(t / 86400);
  137. hour = Convert.ToInt32((t % 86400) / 3600);
  138. minute = Convert.ToInt32((t % 86400 % 3600) / 60);
  139. second = Convert.ToInt32(t % 86400 % 3600 % 60);
  140. if (type == 0)
  141. r = day + ("天") + hour + ("小时") + minute + ("分") + second + ("秒");
  142. else
  143. r = day + ("天") + hour + ("小时") + minute + ("分");
  144. }
  145. else if (t >= 3600)//时,
  146. {
  147. hour = Convert.ToInt32(t / 3600);
  148. minute = Convert.ToInt32((t % 3600) / 60);
  149. second = Convert.ToInt32(t % 3600 % 60);
  150. if (type == 0)
  151. r = hour + ("小时") + minute + ("分") + second + ("秒");
  152. else
  153. r = hour + ("小时") + minute + ("分");
  154. }
  155. else if (t >= 60)//分
  156. {
  157. minute = Convert.ToInt32(t / 60);
  158. second = Convert.ToInt32(t % 60);
  159. r = minute + ("分") + second + ("秒");
  160. }
  161. else
  162. {
  163. second = Convert.ToInt32(t);
  164. r = second + ("秒");
  165. }
  166. return r;
  167. }
  168. ///<summary>
  169. ///由秒数得到日期几天几小时。。。
  170. ///</summary
  171. ///<param name="t">秒数</param>
  172. ///<param name="type">0:转换后带秒,1:转换后不带秒</param>
  173. ///<returns>几天几小时几分几秒</returns>
  174. public static string parseTimeSeconds(double t, double type)
  175. {
  176. string r = "";
  177. double day, hour, minute, second;
  178. if (t >= 86400) //天,
  179. {
  180. day = Convert.ToInt32(Math.Floor(t / 86400));
  181. hour = Convert.ToInt32(Math.Floor((t % 86400) / 3600));
  182. minute = Convert.ToInt32(Math.Floor((t % 86400 % 3600) / 60));
  183. second = Convert.ToInt32(Math.Floor(t % 86400 % 3600 % 60));
  184. if (type == 0)
  185. r = day + ("天") + hour + ("小时") + minute + ("分") + second + ("秒");
  186. else
  187. r = day + ("天") + hour + ("小时") + minute + ("分");
  188. }
  189. else if (t >= 3600)//时,
  190. {
  191. hour = Convert.ToInt32(Math.Floor(t / 3600));
  192. minute = Convert.ToInt32(Math.Floor((t % 3600) / 60));
  193. second = Convert.ToInt32(Math.Floor(t % 3600 % 60));
  194. if (type == 0)
  195. r = hour + ("小时") + minute + ("分") + second + ("秒");
  196. else
  197. r = hour + ("小时") + minute + ("分");
  198. }
  199. else if (t >= 60)//分
  200. {
  201. minute = Convert.ToInt32(Math.Floor(t / 60));
  202. second = Convert.ToInt32(Math.Floor(t % 60));
  203. r = minute + ("分") + second + ("秒");
  204. }
  205. else
  206. {
  207. second = Convert.ToInt32(t);
  208. r = second + ("秒");
  209. }
  210. return r;
  211. }
  212. /// <summary>
  213. /// 当前日期时间,转换为秒
  214. /// </summary>
  215. /// <returns>秒数</returns>
  216. public static string xDateSeconds()
  217. {
  218. long xdateseconds = 0;
  219. DateTime xdatenow = DateTime.UtcNow; //当前UTC时间
  220. long xminute = 60; //一分种60秒
  221. long xhour = 3600;
  222. long xday = 86400;
  223. long byear = 1970;//从1970-1-1 0:00:00开始到现在所过的秒
  224. long[] xmonth = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  225. long[] xyear = { 365, 366 };
  226. long num = 0;
  227. xdateseconds += xdatenow.Second; //算秒
  228. xdateseconds += xdatenow.Minute * xminute; //算分
  229. xdateseconds += xdatenow.Hour * xhour; //算时
  230. xdateseconds += (xdatenow.Day - 1) * xday; //算天
  231. //算月(月换成天算)
  232. if (DateTime.IsLeapYear(xdatenow.Year))
  233. {
  234. xdateseconds += (xmonth[xdatenow.Month - 1] + 1) * xday;
  235. }
  236. else
  237. {
  238. xdateseconds += (xmonth[xdatenow.Month - 1]) * xday;
  239. }
  240. //算年(年换成天算)
  241. long lyear = xdatenow.Year - byear;
  242. for (int i = 0; i < lyear; i++)
  243. {
  244. if (DateTime.IsLeapYear((int)byear + i))
  245. {
  246. num++;
  247. }
  248. }
  249. xdateseconds += ((lyear - num) * xyear[0] + num * xyear[1]) * xday;
  250. return xdateseconds.ToString();
  251. }
  252. /// <summary>
  253. /// 秒数转日期
  254. /// </summary>
  255. /// <param name="Value">秒数</param>
  256. /// <returns>日期</returns>
  257. public static DateTime GetGMTDateTime(int Value)
  258. {
  259. //秒数转时间日期
  260. //GMT时间从2000年1月1日开始,先把它作为赋为初值
  261. long Year = 2000, Month = 1, Day = 1;
  262. long Hour = 0, Min = 0, Sec = 0;
  263. //临时变量
  264. long iYear = 0, iDay = 0;
  265. long iHour = 0, iMin = 0, iSec = 0;
  266. //计算文件创建的年份
  267. iYear = Value / (365 * 24 * 60 * 60);
  268. Year = Year + iYear;
  269. //计算文件除创建整年份以外还有多少天
  270. iDay = (Value % (365 * 24 * 60 * 60)) / (24 * 60 * 60);
  271. //把闰年的年份数计算出来
  272. int RInt = 0;
  273. for (int i = 2000; i < Year; i++)
  274. {
  275. if ((i % 4) == 0)
  276. RInt = RInt + 1;
  277. }
  278. //计算文件创建的时间(几时)
  279. iHour = ((Value % (365 * 24 * 60 * 60)) % (24 * 60 * 60)) / (60 * 60);
  280. Hour = Hour + iHour;
  281. //计算文件创建的时间(几分)
  282. iMin = (((Value % (365 * 24 * 60 * 60)) % (24 * 60 * 60)) % (60 * 60)) / 60;
  283. Min = Min + iMin;
  284. //计算文件创建的时间(几秒)
  285. iSec = (((Value % (365 * 24 * 60 * 60)) % (24 * 60 * 60)) % (60 * 60)) % 60;
  286. Sec = Sec + iSec;
  287. DateTime t = new DateTime((int)Year, (int)Month, (int)Day, (int)Hour, (int)Min, (int)Sec);
  288. DateTime t1 = t.AddDays(iDay - RInt);
  289. return t1;
  290. }
  291. }
  292. }