TimeHelper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. using System;
  2. using System.Globalization;
  3. using Masuit.Tools.Logging;
  4. namespace Masuit.Tools.DateTimeExt
  5. {
  6. /// <summary>
  7. /// 时间相关操作帮助类
  8. /// </summary>
  9. public static class TimeHelper
  10. {
  11. #region 返回每月的第一天和最后一天
  12. /// <summary>
  13. /// 返回每月的第一天和最后一天
  14. /// </summary>
  15. /// <param name="_"></param>
  16. /// <param name="month">月份</param>
  17. /// <param name="firstDay">第一天</param>
  18. /// <param name="lastDay">最后一天</param>
  19. public static void ReturnDateFormat(this DateTime _, int month, out string firstDay, out string lastDay)
  20. {
  21. int year = DateTime.Now.Year + month / 12;
  22. if (month != 12) month %= 12;
  23. switch (month)
  24. {
  25. case 1:
  26. firstDay = DateTime.Now.ToString($"{year}-0{month}-01");
  27. lastDay = DateTime.Now.ToString($"{year}-0{month}-31");
  28. break;
  29. case 2:
  30. firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
  31. if (DateTime.IsLeapYear(DateTime.Now.Year)) lastDay = DateTime.Now.ToString(year + "-0" + month + "-29");
  32. else lastDay = DateTime.Now.ToString(year + "-0" + month + "-28");
  33. break;
  34. case 3:
  35. firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
  36. lastDay = DateTime.Now.ToString("yyyy-0" + month + "-31");
  37. break;
  38. case 4:
  39. firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
  40. lastDay = DateTime.Now.ToString(year + "-0" + month + "-30");
  41. break;
  42. case 5:
  43. firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
  44. lastDay = DateTime.Now.ToString(year + "-0" + month + "-31");
  45. break;
  46. case 6:
  47. firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
  48. lastDay = DateTime.Now.ToString(year + "-0" + month + "-30");
  49. break;
  50. case 7:
  51. firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
  52. lastDay = DateTime.Now.ToString(year + "-0" + month + "-31");
  53. break;
  54. case 8:
  55. firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
  56. lastDay = DateTime.Now.ToString(year + "-0" + month + "-31");
  57. break;
  58. case 9:
  59. firstDay = DateTime.Now.ToString(year + "-0" + month + "-01");
  60. lastDay = DateTime.Now.ToString(year + "-0" + month + "-30");
  61. break;
  62. case 10:
  63. firstDay = DateTime.Now.ToString(year + "-" + month + "-01");
  64. lastDay = DateTime.Now.ToString(year + "-" + month + "-31");
  65. break;
  66. case 11:
  67. firstDay = DateTime.Now.ToString(year + "-" + month + "-01");
  68. lastDay = DateTime.Now.ToString(year + "-" + month + "-30");
  69. break;
  70. default:
  71. firstDay = DateTime.Now.ToString(year + "-" + month + "-01");
  72. lastDay = DateTime.Now.ToString(year + "-" + month + "-31");
  73. break;
  74. }
  75. }
  76. #endregion
  77. #region 将时间格式化成 年月日 的形式,如果时间为null,返回当前系统时间
  78. /// <summary>
  79. /// 将时间格式化成 年月日 的形式,如果时间为null,返回当前系统时间
  80. /// </summary>
  81. /// <param name="dt">年月日分隔符</param>
  82. /// <param name="separator">分隔符</param>
  83. /// <returns>xxxx年xx月xx日</returns>
  84. public static string GetFormatDate(this DateTime dt, char separator)
  85. {
  86. if (dt != null && !dt.Equals(DBNull.Value))
  87. {
  88. string tem = $"yyyy{separator}MM{separator}dd";
  89. return dt.ToString(tem);
  90. }
  91. return GetFormatDate(DateTime.Now, separator);
  92. }
  93. #endregion
  94. #region 将时间格式化成 时分秒 的形式,如果时间为null,返回当前系统时间
  95. /// <summary>
  96. /// 将时间格式化成 时分秒 的形式,如果时间为null,返回当前系统时间
  97. /// </summary>
  98. /// <param name="dt">当前日期时间对象</param>
  99. /// <param name="separator">分隔符</param>
  100. /// <returns> xx时xx分xx秒 </returns>
  101. public static string GetFormatTime(this DateTime dt, char separator)
  102. {
  103. if (dt != null && !dt.Equals(DBNull.Value))
  104. {
  105. string tem = string.Format("hh{0}mm{1}ss", separator, separator);
  106. return dt.ToString(tem);
  107. }
  108. return GetFormatDate(DateTime.Now, separator);
  109. }
  110. #endregion
  111. #region 把秒转换成分钟
  112. /// <summary>
  113. /// 把秒转换成分钟
  114. /// </summary>
  115. /// <param name="_"></param>
  116. /// <param name="second">秒数</param>
  117. /// <returns>分钟数</returns>
  118. public static int SecondToMinute(this DateTime _, int second)
  119. {
  120. decimal mm = second / (decimal)60;
  121. return Convert.ToInt32(Math.Ceiling(mm));
  122. }
  123. #endregion
  124. #region 返回某年某月最后一天
  125. /// <summary>
  126. /// 返回某年某月最后一天
  127. /// </summary>
  128. /// <param name="_"></param>
  129. /// <param name="year">年份</param>
  130. /// <param name="month">月份</param>
  131. /// <returns>日</returns>
  132. public static int GetMonthLastDate(this DateTime _, int year, int month)
  133. {
  134. DateTime lastDay = new DateTime(year, month, new GregorianCalendar().GetDaysInMonth(year, month));
  135. int day = lastDay.Day;
  136. return day;
  137. }
  138. #endregion
  139. #region 获得两个日期的间隔
  140. /// <summary>
  141. /// 获得两个日期的间隔
  142. /// </summary>
  143. /// <param name="dateTime1">日期一。</param>
  144. /// <param name="dateTime2">日期二。</param>
  145. /// <returns>日期间隔TimeSpan。</returns>
  146. /// <exception cref="OverflowException">The return value is less than <see cref="F:System.TimeSpan.MinValue" /> or greater than <see cref="F:System.TimeSpan.MaxValue" />. </exception>
  147. public static TimeSpan DateDiff2(this DateTime dateTime1, DateTime dateTime2)
  148. {
  149. TimeSpan ts1 = new TimeSpan(dateTime1.Ticks);
  150. TimeSpan ts2 = new TimeSpan(dateTime2.Ticks);
  151. TimeSpan ts = ts1.Subtract(ts2).Duration();
  152. return ts;
  153. }
  154. #endregion
  155. #region 格式化日期时间
  156. /// <summary>
  157. /// 格式化日期时间
  158. /// </summary>
  159. /// <param name="dateTime1">日期时间</param>
  160. /// <param name="dateMode">显示模式</param>
  161. /// <returns>0-9种模式的日期</returns>
  162. public static string FormatDate(this DateTime dateTime1, string dateMode)
  163. {
  164. switch (dateMode)
  165. {
  166. case "0": return dateTime1.ToString("yyyy-MM-dd");
  167. case "1": return dateTime1.ToString("yyyy-MM-dd HH:mm:ss");
  168. case "2": return dateTime1.ToString("yyyy/MM/dd");
  169. case "3": return dateTime1.ToString("yyyy年MM月dd日");
  170. case "4": return dateTime1.ToString("MM-dd");
  171. case "5": return dateTime1.ToString("MM/dd");
  172. case "6": return dateTime1.ToString("MM月dd日");
  173. case "7": return dateTime1.ToString("yyyy-MM");
  174. case "8": return dateTime1.ToString("yyyy/MM");
  175. case "9": return dateTime1.ToString("yyyy年MM月");
  176. default: return dateTime1.ToString(CultureInfo.CurrentCulture);
  177. }
  178. }
  179. #endregion
  180. #region 得到随机日期
  181. /// <summary>
  182. /// 得到随机日期
  183. /// </summary>
  184. /// <param name="time1">起始日期</param>
  185. /// <param name="time2">结束日期</param>
  186. /// <returns>间隔日期之间的 随机日期</returns>
  187. public static DateTime GetRandomTime(this DateTime time1, DateTime time2)
  188. {
  189. Random random = new Random();
  190. DateTime minTime;
  191. TimeSpan ts = new TimeSpan(time1.Ticks - time2.Ticks);
  192. // 获取两个时间相隔的秒数
  193. double dTotalSecontds = ts.TotalSeconds;
  194. int iTotalSecontds;
  195. if (dTotalSecontds > int.MaxValue) iTotalSecontds = int.MaxValue;
  196. else if (dTotalSecontds < int.MinValue) iTotalSecontds = int.MinValue;
  197. else iTotalSecontds = (int)dTotalSecontds;
  198. if (iTotalSecontds > 0)
  199. {
  200. minTime = time2;
  201. }
  202. else if (iTotalSecontds < 0)
  203. {
  204. minTime = time1;
  205. }
  206. else
  207. {
  208. return time1;
  209. }
  210. int maxValue = iTotalSecontds;
  211. if (iTotalSecontds <= int.MinValue) maxValue = int.MinValue + 1;
  212. int i = random.Next(Math.Abs(maxValue));
  213. return minTime.AddSeconds(i);
  214. }
  215. #endregion
  216. #region Rss日期时间转换,将时间全部转换为GMT时间
  217. /// <summary>
  218. /// Rss日期时间转换,将时间全部转换为GMT时间
  219. /// </summary>
  220. /// <param name="strDateTime">Rss中读取的时间</param>
  221. /// <returns>处理后的标准时间格式</returns>
  222. public static string DateConvert(this string strDateTime)
  223. {
  224. strDateTime = strDateTime.Replace("+0000", "GMT");
  225. strDateTime = strDateTime.Replace("+0100", "GMT");
  226. strDateTime = strDateTime.Replace("+0200", "GMT");
  227. strDateTime = strDateTime.Replace("+0300", "GMT");
  228. strDateTime = strDateTime.Replace("+0400", "GMT");
  229. strDateTime = strDateTime.Replace("+0500", "GMT");
  230. strDateTime = strDateTime.Replace("+0600", "GMT");
  231. strDateTime = strDateTime.Replace("+0700", "GMT");
  232. strDateTime = strDateTime.Replace("+0800", "GMT");
  233. strDateTime = strDateTime.Replace("-0000", "GMT");
  234. strDateTime = strDateTime.Replace("-0100", "GMT");
  235. strDateTime = strDateTime.Replace("-0200", "GMT");
  236. strDateTime = strDateTime.Replace("-0300", "GMT");
  237. strDateTime = strDateTime.Replace("-0400", "GMT");
  238. strDateTime = strDateTime.Replace("-0500", "GMT");
  239. strDateTime = strDateTime.Replace("-0600", "GMT");
  240. strDateTime = strDateTime.Replace("-0700", "GMT");
  241. strDateTime = strDateTime.Replace("-0800", "GMT");
  242. DateTime dt = DateTime.Parse(strDateTime, null, DateTimeStyles.AdjustToUniversal);
  243. return dt.ToString();
  244. }
  245. #endregion
  246. #region 时间相关操作类
  247. /// <summary>
  248. /// 获得一段时间内有多少小时
  249. /// </summary>
  250. /// <param name="dtStar">起始时间</param>
  251. /// <param name="dtEnd">终止时间</param>
  252. /// <returns>小时差</returns>
  253. public static string GetTimeDelay(this DateTime dtStar, DateTime dtEnd)
  254. {
  255. long lTicks = (dtEnd.Ticks - dtStar.Ticks) / 10000000;
  256. string sTemp = (lTicks / 3600).ToString().PadLeft(2, '0') + ":";
  257. sTemp += (lTicks % 3600 / 60).ToString().PadLeft(2, '0') + ":";
  258. sTemp += (lTicks % 3600 % 60).ToString().PadLeft(2, '0');
  259. return sTemp;
  260. }
  261. /// <summary>
  262. /// 获得8位时间整型数字
  263. /// </summary>
  264. /// <param name="dt">当前的日期时间对象</param>
  265. /// <returns>8位时间整型数字</returns>
  266. public static string GetDateString(this DateTime dt)
  267. {
  268. return dt.Year + dt.Month.ToString().PadLeft(2, '0') + dt.Day.ToString().PadLeft(2, '0');
  269. }
  270. #endregion
  271. #region 返回时间差
  272. /// <summary>
  273. /// 返回时间差
  274. /// </summary>
  275. /// <param name="dateTime1">时间1</param>
  276. /// <param name="dateTime2">时间2</param>
  277. /// <returns>时间差</returns>
  278. public static string DateDiff(this DateTime dateTime1, DateTime dateTime2)
  279. {
  280. string dateDiff = null;
  281. try
  282. {
  283. TimeSpan ts = dateTime2 - dateTime1;
  284. if (ts.Days >= 1)
  285. {
  286. dateDiff = dateTime1.Month + "月" + dateTime1.Day + "日";
  287. }
  288. else
  289. {
  290. if (ts.Hours > 1) dateDiff = ts.Hours + "小时前";
  291. else dateDiff = ts.Minutes + "分钟前";
  292. }
  293. }
  294. catch (Exception e)
  295. {
  296. LogManager.Error(e);
  297. }
  298. return dateDiff;
  299. }
  300. /// <summary>
  301. /// 时间差
  302. /// </summary>
  303. /// <param name="beginTime">开始时间</param>
  304. /// <param name="endTime">结束时间</param>
  305. /// <returns>时间差</returns>
  306. public static string GetDiffTime(this DateTime beginTime, DateTime endTime)
  307. {
  308. int i = 0;
  309. return GetDiffTime(beginTime, endTime, ref i);
  310. }
  311. /// <summary>
  312. /// 计算2个时间差
  313. /// </summary>
  314. /// <param name="beginTime">开始时间</param>
  315. /// <param name="endTime">结束时间</param>
  316. /// <param name="mindTime">中间的时间</param>
  317. /// <returns>时间差</returns>
  318. public static string GetDiffTime(this DateTime beginTime, DateTime endTime, ref int mindTime)
  319. {
  320. string strResout = string.Empty;
  321. //获得2时间的时间间隔秒计算
  322. TimeSpan span = endTime.Subtract(beginTime);
  323. int iTatol = Convert.ToInt32(span.TotalSeconds);
  324. int iMinutes = 1 * 60;
  325. int iHours = iMinutes * 60;
  326. int iDay = iHours * 24;
  327. int iMonth = iDay * 30;
  328. int iYear = iMonth * 12;
  329. //提醒时间,到了返回1,否则返回0
  330. if (mindTime > iTatol && iTatol > 0) mindTime = 1;
  331. else mindTime = 0;
  332. if (iTatol > iYear)
  333. {
  334. strResout += iTatol / iYear + "年";
  335. iTatol %= iYear; //剩余
  336. }
  337. if (iTatol > iMonth)
  338. {
  339. strResout += iTatol / iMonth + "月";
  340. iTatol %= iMonth;
  341. }
  342. if (iTatol > iDay)
  343. {
  344. strResout += iTatol / iDay + "天";
  345. iTatol %= iDay;
  346. }
  347. if (iTatol > iHours)
  348. {
  349. strResout += iTatol / iHours + "小时";
  350. iTatol %= iHours;
  351. }
  352. if (iTatol > iMinutes)
  353. {
  354. strResout += iTatol / iMinutes + "分";
  355. iTatol %= iMinutes;
  356. }
  357. strResout += iTatol + "秒";
  358. return strResout;
  359. }
  360. #endregion
  361. #region 时间其他转换静态方法
  362. /// <summary>
  363. /// C#的时间到Javascript的时间的转换
  364. /// </summary>
  365. /// <param name="TheDate">C#的时间</param>
  366. /// <returns>Javascript的时间</returns>
  367. public static long CsharpTime2JavascriptTime(this DateTime TheDate)
  368. {
  369. DateTime d1 = new DateTime(1970, 1, 1);
  370. DateTime d2 = TheDate.ToUniversalTime();
  371. TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
  372. return (long)ts.TotalMilliseconds;
  373. }
  374. /// <summary>
  375. /// PHP的时间转换成C#中的DateTime
  376. /// </summary>
  377. /// <param name="_"></param>
  378. /// <param name="time">php的时间</param>
  379. /// <returns>C#的时间</returns>
  380. public static DateTime PhpTime2CsharpTime(this DateTime _, long time)
  381. {
  382. DateTime timeStamp = new DateTime(1970, 1, 1); //得到1970年的时间戳
  383. long t = (time + 8 * 60 * 60) * 10000000 + timeStamp.Ticks;
  384. return new DateTime(t);
  385. }
  386. /// <summary>
  387. /// C#中的DateTime转换成PHP的时间
  388. /// </summary>
  389. /// <param name="time">C#时间</param>
  390. /// <returns>php时间</returns>
  391. public static long CsharpTime2PhpTime(this DateTime time)
  392. {
  393. DateTime timeStamp = new DateTime(1970, 1, 1); //得到1970年的时间戳
  394. //注意这里有时区问题,用now就要减掉8个小时
  395. return (DateTime.UtcNow.Ticks - timeStamp.Ticks) / 10000000;
  396. }
  397. #endregion
  398. }
  399. }