TimeHelper.cs 15 KB

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