Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DateTimeUtil.cs 25KB

před 1 rokem
před 1 měsícem
před 1 měsícem
před 1 rokem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace HealthMonitor.Common
  7. {
  8. public class DateTimeUtil
  9. {
  10. /// <summary>
  11. /// 时间类型转成时间戳
  12. /// </summary>
  13. /// <param name="time">Local Time</param>
  14. /// <returns></returns>
  15. public static long ConvertToTimeStamp(DateTime time)
  16. {
  17. long intResult = 0;
  18. DateTime startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local);
  19. intResult = (long)(time - startTime).TotalMilliseconds;
  20. return intResult;
  21. }
  22. /// <summary>
  23. /// 毫秒时间戳转本地时间
  24. /// </summary>
  25. /// <param name="milliseconds"></param>
  26. /// <returns></returns>
  27. public static DateTime GetDateTimeFromUnixTimeMilliseconds(long milliseconds)
  28. {
  29. DateTimeOffset dt0 = DateTimeOffset.FromUnixTimeMilliseconds(milliseconds);
  30. //默认为UTC时间:{2019/11/14 1:53:26}
  31. //DateTime dt1 = dt0.DateTime;
  32. //转为本地时区:{2019/11/14 9:53:26}
  33. DateTime dt2 = dt0.LocalDateTime;
  34. return dt2;
  35. }
  36. /// <summary>
  37. /// 判断给定的日期时间字符串是否符合相应的格式
  38. /// </summary>
  39. /// <param name="strDate">日期时间字符串</param>
  40. /// <returns>是否符合格式</returns>
  41. public static bool IsDateTime(string strDate)
  42. {
  43. if (strDate.Length != 19)
  44. return false;
  45. try
  46. {
  47. DateTime dt = DateTime.Parse(strDate);
  48. return true;
  49. }
  50. catch
  51. {
  52. return false;
  53. }
  54. }
  55. /// <summary>
  56. /// 得到当前月的第一天
  57. /// </summary>
  58. /// <returns>当前月的第一天</returns>
  59. public static string ThisMonthFirstDate
  60. {
  61. get
  62. {
  63. DateTime dt = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
  64. return dt.ToString("yyyy-MM-dd HH:mm:ss");
  65. }
  66. }
  67. /// <summary>
  68. /// 得到当前月的最后一天
  69. /// </summary>
  70. /// <returns>当前月的最后一天</returns>
  71. public static string ThisMonthLastDate
  72. {
  73. get
  74. {
  75. DateTime dt = new DateTime(DateTime.Today.Year, DateTime.Today.Month + 1, 1);
  76. dt = dt.AddDays(-1);
  77. return dt.ToString("yyyy-MM-dd HH:mm:ss");
  78. }
  79. }
  80. /// <summary>
  81. /// 指定日期的该月第一天
  82. /// </summary>
  83. /// <param name="sThisDate">指定的日期</param>
  84. /// <returns>该月第一天</returns>
  85. public static string MonthFirstDate(string sThisDate)
  86. {
  87. int nYear = Year(sThisDate);
  88. int nMonth = Month(sThisDate);
  89. int nDay = 1;
  90. return EncodeDate(nYear, nMonth, nDay) + " 00:00:00";
  91. }
  92. /// <summary>
  93. /// 指定日期的该月最后一天
  94. /// </summary>
  95. /// <param name="sThisDate">指定的日期</param>
  96. /// <returns>该月最后一天</returns>
  97. public static string MonthLastDate(string sThisDate)
  98. {
  99. int nYear = Year(sThisDate);
  100. int nMonth = Month(sThisDate);
  101. nMonth++;
  102. if (nMonth == 13)
  103. {
  104. nYear++;
  105. nMonth = 1;
  106. }
  107. DateTime dt = new DateTime(nYear, nMonth, 1);
  108. dt = dt.AddDays(-1);
  109. return dt.ToString("yyyy-MM-dd HH:mm:ss");
  110. }
  111. /// <summary>
  112. /// 转换字符串类型的时间至日期时间类型
  113. /// </summary>
  114. /// <param name="strDateTime">时间字符串</param>
  115. /// <returns>日期时间类型的值</returns>
  116. /// <remarks>如果格式不正确,则返回当前的时间</remarks>
  117. public static DateTime ToDateTime(string strDateTime)
  118. {
  119. try
  120. {
  121. return DateTime.Parse(strDateTime);
  122. }
  123. catch
  124. {
  125. return DateTime.Now;
  126. }
  127. }
  128. /// <summary>
  129. /// 转换时间类型的字符串至日期时间类型
  130. /// </summary>
  131. /// <param name="strTime">时间字符串</param>
  132. /// <returns>时间值</returns>
  133. /// <remarks>如果格式不正确,则返回的是"00:00:00"的时间值</remarks>
  134. public static DateTime ToTime(string strTime)
  135. {
  136. try
  137. {
  138. return DateTime.Parse(strTime);
  139. }
  140. catch
  141. {
  142. return DateTime.Parse("00:00:00");
  143. }
  144. }
  145. /// <summary>
  146. /// 将时间值转换至时间字符串
  147. /// </summary>
  148. /// <param name="time">时间值</param>
  149. /// <returns>时间字符串</returns>
  150. public static string ToTimeStr(DateTime time)
  151. {
  152. return time.ToString("HH:mm:ss");
  153. }
  154. /// <summary>
  155. /// 将对象转换为指定格式的字符串
  156. /// </summary>
  157. /// <param name="time">待转换的对象</param>
  158. /// <param name="sFormat">指定的字符串格式</param>
  159. /// <returns>转换后的字符串</returns>
  160. /// <remarks>如果为空值,或非日期时间对象,则返回""</remarks>
  161. public static string ToTimeStr(object time, string sFormat)
  162. {
  163. if (time == null || time == DBNull.Value)
  164. return "";
  165. else
  166. {
  167. try
  168. {
  169. return ((DateTime)time).ToString(sFormat);
  170. }
  171. catch
  172. {
  173. return "";
  174. }
  175. }
  176. }
  177. /// <summary>
  178. /// 将对象转换为字符串,格式为"HH:mm:ss"
  179. /// </summary>
  180. /// <param name="time">待转换的对象</param>
  181. /// <returns>转换后的字符串</returns>
  182. public static string ToTimeStr(object time)
  183. {
  184. return ToTimeStr(time, "HH:mm:ss");
  185. }
  186. /// <summary>
  187. /// 从秒数转换为时间字符串
  188. /// </summary>
  189. /// <param name="Second">秒数</param>
  190. /// <returns>时间字符串</returns>
  191. public static string ToTimeStrFromSecond(int Second)
  192. {
  193. //=========== 1. 得到小时、分钟和秒数 ===========
  194. string sTimeStr = "";
  195. int NewSecond = 0;
  196. int hour = Math.DivRem(Second, 3600, out NewSecond); //小时
  197. Second = NewSecond;
  198. NewSecond = 0;
  199. int minute = Math.DivRem(Second, 60, out NewSecond); //分钟
  200. //============ 2. 得到返回的字符串 ============
  201. if (hour < 10)
  202. sTimeStr = sTimeStr + "0" + hour.ToString() + ":";
  203. else
  204. sTimeStr = sTimeStr + hour.ToString() + ":";
  205. if (minute < 10)
  206. sTimeStr = sTimeStr + "0" + minute.ToString() + ":";
  207. else
  208. sTimeStr = sTimeStr + minute.ToString() + ":";
  209. if (NewSecond < 10)
  210. sTimeStr = sTimeStr + "0" + NewSecond.ToString();
  211. else
  212. sTimeStr = sTimeStr + NewSecond.ToString();
  213. return sTimeStr;
  214. }
  215. /// <summary>
  216. /// 将时间字符串转换为秒数
  217. /// </summary>
  218. /// <param name="timeStr">时间字符串</param>
  219. /// <returns>转换后的秒数</returns>
  220. public static int ToSecondsFromTimeStr(string timeStr)
  221. {
  222. DateTime dt = ToTime(timeStr);
  223. return dt.Hour * 3600 + dt.Minute * 60 + dt.Second;
  224. }
  225. /// <summary>
  226. /// 将日期时间值转换为日期字符串
  227. /// </summary>
  228. /// <param name="dt">日期时间值</param>
  229. /// <returns>日期字符串</returns>
  230. public static string ToDateStr(DateTime dt)
  231. {
  232. return dt.ToString("yyyy-MM-dd");
  233. }
  234. /// <summary>
  235. /// 将对象转换为日期字符串
  236. /// </summary>
  237. /// <param name="dt">对象</param>
  238. /// <param name="defaultStr">缺省字符串</param>
  239. /// <returns>转换后的字符串。如果为空值,则按缺省值返回</returns>
  240. public static string ToDateStr(object dt, string defaultStr)
  241. {
  242. if (dt == null || dt is System.DBNull)
  243. return defaultStr;
  244. else
  245. return ((DateTime)dt).ToString("yyyy-MM-dd");
  246. }
  247. /// <summary>
  248. /// 将对象转换为日期字符串
  249. /// </summary>
  250. /// <param name="dt">对象</param>
  251. /// <returns>转换后的字符串。如果转换不成功,则返回""</returns>
  252. public static string ToDateStr(object dt)
  253. {
  254. return ToDateStr(dt, "");
  255. }
  256. /// <summary>
  257. /// 将日期时间值转换为字符串
  258. /// </summary>
  259. /// <param name="dt">日期时间值</param>
  260. /// <returns>字符串(len=19)</returns>
  261. public static string ToDateTimeStr(DateTime dt)
  262. {
  263. return dt.ToString("yyyy-MM-dd HH:mm:ss");
  264. }
  265. public static string ToDateTimeStrWithMilliSeconds(DateTime dt)
  266. {
  267. int nMilliSeconds = dt.Millisecond;
  268. return dt.ToString("yyyy-MM-dd HH:mm:ss") + "." + nMilliSeconds.ToString("000");
  269. }
  270. /// <summary>
  271. /// 将对象转换为日期时间字符串
  272. /// </summary>
  273. /// <param name="dt">对象</param>
  274. /// <param name="defaultStr">转换不成功时所取的缺省字符串</param>
  275. /// <returns>转换后的日期时间字符串(len=19)</returns>
  276. public static string ToDateTimeStr(object dt, string defaultStr)
  277. {
  278. return ToDateTimeStr(dt, defaultStr, "yyyy-MM-dd HH:mm:ss");
  279. }
  280. /// <summary>
  281. /// 将对象转换为日期时间字符串
  282. /// </summary>
  283. /// <param name="dt">对象</param>
  284. /// <returns>转换后的日期时间字符串(len=19)</returns>
  285. /// <remarks>如果转换不成功,则返回""</remarks>
  286. public static string ToDateTimeStr(object dt)
  287. {
  288. return ToDateTimeStr(dt, "", "yyyy-MM-dd HH:mm:ss");
  289. }
  290. /// <summary>
  291. /// 将对象转换为指定格式的日期时间字符串
  292. /// </summary>
  293. /// <param name="dt">对象</param>
  294. /// <param name="defaultStr">缺省字符串</param>
  295. /// <param name="sFormat">格式</param>
  296. /// <returns>转换后的字符串</returns>
  297. public static string ToDateTimeStr(object dt, string defaultStr, string sFormat)
  298. {
  299. if (dt == null || dt is System.DBNull)
  300. return defaultStr;
  301. else
  302. return ((DateTime)dt).ToString(sFormat);
  303. }
  304. /// <summary>
  305. /// 获取当前的时间
  306. /// </summary>
  307. public static string Now
  308. {
  309. get
  310. {
  311. return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  312. }
  313. }
  314. /// <summary>
  315. /// 带毫秒部分的当前时间,示例为2003.07.22 10:02:52.136
  316. /// </summary>
  317. public static string NowWithMilliSeconds
  318. {
  319. get
  320. {
  321. DateTime dtNow = DateTime.Now;
  322. int nMilliSeconds = dtNow.Millisecond;
  323. return dtNow.ToString("yyyy-MM-dd HH:mm:ss") + "." + nMilliSeconds.ToString("000");
  324. }
  325. }
  326. /// <summary>
  327. /// 获取当前的时间
  328. /// </summary>
  329. public static string Today
  330. {
  331. get
  332. {
  333. return DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss");
  334. }
  335. }
  336. /// <summary>
  337. /// 获取日期的年份
  338. /// </summary>
  339. /// <param name="dtString">日期时间串</param>
  340. /// <returns>年份</returns>
  341. public static int Year(string dtString)
  342. {
  343. int nYear;
  344. nYear = Convert.ToInt32(dtString.Substring(0, 4));
  345. return nYear;
  346. }
  347. /// <summary>
  348. /// 获取日期的月份
  349. /// </summary>
  350. /// <param name="dtString">日期时间串</param>
  351. /// <returns>月份</returns>
  352. public static int Month(string dtString)
  353. {
  354. int nMonth;
  355. nMonth = Convert.ToInt32(dtString.Substring(5, 2));
  356. return nMonth;
  357. }
  358. /// <summary>
  359. /// 获取日期中该月的第几天
  360. /// </summary>
  361. /// <param name="dtString">日期时间串</param>
  362. /// <returns>该月的第几天</returns>
  363. public static int Day(string dtString)
  364. {
  365. int nDay;
  366. nDay = Convert.ToInt32(dtString.Substring(8, 2));
  367. return nDay;
  368. }
  369. /// <summary>
  370. /// 获取日期中的小时部分
  371. /// </summary>
  372. /// <param name="dtString">日期时间串</param>
  373. /// <returns>小时部分</returns>
  374. public static int Hour(string dtString)
  375. {
  376. int nHour;
  377. if (dtString.Length != 19 && dtString.Length != 23)
  378. return 0;
  379. nHour = Convert.ToInt32(dtString.Substring(11, 2));
  380. return nHour;
  381. }
  382. /// <summary>
  383. /// 获取日期中的分钟部分
  384. /// </summary>
  385. /// <param name="dtString">日期时间串</param>
  386. /// <returns>分钟部分</returns>
  387. public static int Minute(string dtString)
  388. {
  389. int nMinute;
  390. if (dtString.Length != 19 && dtString.Length != 23)
  391. return 0;
  392. nMinute = Convert.ToInt32(dtString.Substring(14, 2));
  393. return nMinute;
  394. }
  395. /// <summary>
  396. /// 获取日期中的秒部分
  397. /// </summary>
  398. /// <param name="dtString">日期时间串</param>
  399. /// <returns>秒部分</returns>
  400. public static int Second(string dtString)
  401. {
  402. int nSecond;
  403. if (dtString.Length != 19 && dtString.Length != 23)
  404. return 0;
  405. nSecond = Convert.ToInt32(dtString.Substring(17, 2));
  406. return nSecond;
  407. }
  408. /// <summary>
  409. /// 获取表示的日期是星期几
  410. /// </summary>
  411. /// <param name="dtString">日期时间串</param>
  412. /// <returns>星期几</returns>
  413. /// <remarks>返回值从0至6;星期日为0,星期六为6</remarks>
  414. public static int DayOfWeek(string dtString)
  415. {
  416. DateTime dt = DateTime.Parse(dtString);
  417. return (int)dt.DayOfWeek;
  418. }
  419. /// <summary>
  420. /// 获取表示的日期是一年中的第几天
  421. /// </summary>
  422. /// <param name="dtString">日期时间串</param>
  423. /// <returns>一年中的第几天</returns>
  424. public static int DayOfYear(string dtString)
  425. {
  426. DateTime dt = DateTime.Parse(dtString);
  427. return dt.DayOfYear;
  428. }
  429. /// <summary>
  430. /// 将指定的月份数加到日期值上
  431. /// </summary>
  432. /// <param name="dtString">原来的日期</param>
  433. /// <param name="nOffset">指定的月份数</param>
  434. /// <returns>得到的日期</returns>
  435. public static string AddMonths(string dtString, int nOffset)
  436. {
  437. DateTime dt = DateTime.Parse(dtString);
  438. DateTime dtRes = dt.AddMonths(nOffset);
  439. return dtRes.ToString("yyyy-MM-dd HH:mm:ss");
  440. }
  441. /// <summary>
  442. /// 将指定的天数加到日期值上
  443. /// </summary>
  444. /// <param name="dtString">原来的日期</param>
  445. /// <param name="offset">指定的天数</param>
  446. /// <returns>得到的日期</returns>
  447. /// <remarks>天数可以为负数</remarks>
  448. public static string AddDays(string dtString, int offset)
  449. {
  450. DateTime dt = DateTime.Parse(dtString);
  451. DateTime dtRes = dt.AddDays(offset);
  452. return dtRes.ToString("yyyy-MM-dd HH:mm:ss");
  453. }
  454. /// <summary>
  455. /// 将指定的秒数加到日期值上
  456. /// </summary>
  457. /// <param name="dtString">原来的日期时间</param>
  458. /// <param name="offset">指定的秒数</param>
  459. /// <returns>得到的日期时间</returns>
  460. /// <remarks>秒数可以为负数</remarks>
  461. public static string AddSeconds(string dtString, int offset)
  462. {
  463. DateTime dt = DateTime.Parse(dtString);
  464. DateTime dtRes = dt.AddSeconds(offset);
  465. return dtRes.ToString("yyyy-MM-dd HH:mm:ss");
  466. }
  467. public static string AddMilliSeconds(string dtString, int offset)
  468. {
  469. DateTime dt = DateTime.Parse(dtString);
  470. DateTime dtRes = dt.AddMilliseconds(offset);
  471. int nMilliSeconds = dtRes.Millisecond;
  472. return dtRes.ToString("yyyy-MM-dd HH:mm:ss") + "." + nMilliSeconds.ToString("000");
  473. }
  474. public static string AddMilliSeconds(string dtString, double offset)
  475. {
  476. DateTime dt = DateTime.Parse(dtString);
  477. DateTime dtRes = dt.AddMilliseconds(offset);
  478. int nMilliSeconds = dtRes.Millisecond;
  479. return dtRes.ToString("yyyy-MM-dd HH:mm:ss") + "." + nMilliSeconds.ToString("000");
  480. }
  481. /// <summary>
  482. /// 得到两个日期时间之间相差的天数
  483. /// </summary>
  484. /// <param name="dtFromString">起始日期</param>
  485. /// <param name="dtToString">终止日期</param>
  486. /// <returns>相差的天数</returns>
  487. /// <remarks>
  488. /// 不判断时间的情况。只判断日期是否切换,即使时间相差仅数分钟,
  489. /// 只要切换了日期,也计算相差的天数。例如,2005-05-05 23:58:58和
  490. /// 2005-05-06 00:02:03之间所差的天数也是1。
  491. /// </remarks>
  492. public static int DaysAfter(string dtFromString, string dtToString)
  493. {
  494. DateTime dtFrom = DateTime.Parse(dtFromString).Date;
  495. DateTime dtTo = DateTime.Parse(dtToString).Date;
  496. TimeSpan timeSpan = dtTo - dtFrom;
  497. return (int)timeSpan.TotalDays;
  498. }
  499. /// <summary>
  500. /// 得到两个日期时间之间相差的秒数
  501. /// </summary>
  502. /// <param name="dtFromString">起始日期时间</param>
  503. /// <param name="dtToString">终止日期时间</param>
  504. /// <returns>相差的秒数</returns>
  505. public static int SecondsAfter(string dtFromString, string dtToString)
  506. {
  507. DateTime dtFrom = DateTime.Parse(dtFromString);
  508. DateTime dtTo = DateTime.Parse(dtToString);
  509. TimeSpan timeSpan = dtTo - dtFrom;
  510. return (int)timeSpan.TotalSeconds;
  511. }
  512. /// <summary>
  513. /// 得到两个日期时间之间相差的毫秒数
  514. /// </summary>
  515. /// <param name="dtFromString">起始日期时间</param>
  516. /// <param name="dtToString">终止日期时间</param>
  517. /// <returns>相差的毫秒数</returns>
  518. public static int MilliSecondsAfter(string dtFromString, string dtToString)
  519. {
  520. DateTime dtFrom = DateTime.Parse(dtFromString);
  521. DateTime dtTo = DateTime.Parse(dtToString);
  522. TimeSpan timeSpan = dtTo - dtFrom;
  523. return (int)timeSpan.TotalMilliseconds;
  524. }
  525. /// <summary>
  526. /// 通过时、分、秒得到时间串
  527. /// </summary>
  528. /// <param name="nHour">小时</param>
  529. /// <param name="nMinute">分钟</param>
  530. /// <param name="nSecond">秒</param>
  531. /// <returns>时间部分</returns>
  532. public static string EncodeTime(int nHour, int nMinute, int nSecond)
  533. {
  534. string sResult = nHour.ToString("00") + ":" + nMinute.ToString("00")
  535. + ":" + nSecond.ToString("00");
  536. return sResult;
  537. }
  538. /// <summary>
  539. /// 通过年、月、日得到日期部分
  540. /// </summary>
  541. /// <param name="nYear">年份</param>
  542. /// <param name="nMonth">月份</param>
  543. /// <param name="nDay">日</param>
  544. /// <returns>日期部分</returns>
  545. public static string EncodeDate(int nYear, int nMonth, int nDay)
  546. {
  547. string sResult = nYear.ToString("0000") + "-" + nMonth.ToString("00")
  548. + "-" + nDay.ToString("00");
  549. return sResult;
  550. }
  551. /// <summary>
  552. /// 得到日期部分
  553. /// </summary>
  554. /// <param name="sDatetime">日期时间</param>
  555. /// <returns>日期部分</returns>
  556. public static string GetDatePart(string sDatetime)
  557. {
  558. if (sDatetime.Length < 10)
  559. return "";
  560. return sDatetime.Substring(0, 10);
  561. }
  562. /// <summary>
  563. /// 得到时间部分
  564. /// </summary>
  565. /// <param name="sDatetime">日期时间</param>
  566. /// <returns>时间部分</returns>
  567. public static string GetTimePart(string sDatetime)
  568. {
  569. if (sDatetime.Length == 19)
  570. return sDatetime.Substring(11, 8);
  571. else
  572. return "00:00:00";
  573. }
  574. /// <summary>
  575. /// 得到一天的起始时间
  576. /// </summary>
  577. /// <param name="sDate">日期</param>
  578. /// <returns>起始时间</returns>
  579. public static string BeginTimeOfDay(string sDate)
  580. {
  581. if (sDate.Length > 10)
  582. sDate = sDate.Substring(0, 10);
  583. return sDate + " 00:00:00";
  584. }
  585. /// <summary>
  586. /// 得到一天的结束时间
  587. /// </summary>
  588. /// <param name="sDate">日期</param>
  589. /// <returns>结束时间</returns>
  590. public static string EndTimeOfDay(string sDate)
  591. {
  592. if (sDate.Length > 10)
  593. sDate = sDate.Substring(0, 10);
  594. return sDate + " 23:59:59.999";
  595. }
  596. /// <summary>
  597. /// 由19位的日期时间转到14位的日期时间
  598. /// </summary>
  599. /// <param name="sDT">19位的日期时间</param>
  600. /// <returns>14位的日期时间</returns>
  601. public static string ToDateTime14Str(string sDT)
  602. {
  603. if (sDT == "")
  604. return "";
  605. if (sDT.Length != 19)
  606. throw new Exception("DateTimeUtil.ToDateTime14Str() error : 参数不是19位");
  607. string sRet = sDT.Replace(":", "").Replace("-", "").Replace(" ", "");
  608. if (sRet.Length != 14)
  609. throw new Exception("DateTimeUtil.ToDateTime14Str() error : 返回值不是14位");
  610. return sRet;
  611. }
  612. /// <summary>
  613. /// 由14位的日期时间转到19位的日期时间
  614. /// </summary>
  615. /// <param name="sDT14">14位的日期时间</param>
  616. /// <returns>19位的日期时间</returns>
  617. public static string FromDateTime14Str(string sDT14)
  618. {
  619. if (sDT14 == "")
  620. return "";
  621. if (sDT14.Length != 14)
  622. throw new Exception("DateTimeUtil.FromDateTime14Str() error : 参数不是14位");
  623. string sRet = sDT14.Substring(0, 4) + "-" + sDT14.Substring(4, 2)
  624. + "-" + sDT14.Substring(6, 2)
  625. + " " + sDT14.Substring(8, 2) + ":" + sDT14.Substring(10, 2)
  626. + ":" + sDT14.Substring(12, 2);
  627. return sRet;
  628. }
  629. public static double GetTimeDifferenceInSeconds( DateTime end,DateTime start)
  630. {
  631. // 获取时间差
  632. TimeSpan timeDifference = end - start;
  633. // 返回时间差的总秒数
  634. return timeDifference.TotalSeconds;
  635. }
  636. /// <summary>
  637. /// 时间转换逻辑
  638. /// 当前时间是的0~1小时返回2小时 ,当前时间是的1 ~2小时返回3小时 ,如此类推,当前时间是的21 ~22小时返回23小时 ,当前时间是的23 ~0小时返回次日1小时
  639. /// </summary>
  640. /// <param name="currentHour">当前消息</param>
  641. /// <returns></returns>
  642. public static int GetNextHour(int currentHour)
  643. {
  644. // 处理 23 点特殊情况
  645. if (currentHour == 23)
  646. {
  647. return 1; // 23点到0点时,返回次日1小时
  648. }
  649. // 处理 22 点特殊情况
  650. if (currentHour == 22)
  651. {
  652. return 0; // 22点到23点时,返回次日0小时
  653. }
  654. // 计算其他时间的返回值
  655. return (int)((Math.Round(currentHour / 2.0, 1) + 1) * 2);
  656. }
  657. ///// <summary>
  658. ///// 是否为日期型字符串
  659. ///// </summary>
  660. ///// <param name="StrSource">日期字符串(2008-05-08)</param>
  661. ///// <returns></returns>
  662. //public static bool IsDate(string StrSource)
  663. //{
  664. // return Regex.IsMatch(StrSource, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
  665. //}
  666. }
  667. }