TimeHelper.cs 17 KB

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