TimeHelper.cs 15 KB

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