DateTimeEx.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. using System;
  2. using System.Text;
  3. namespace ZcPeng.PublicLibrary
  4. {
  5. /// <summary>
  6. /// DateTimeEx
  7. /// 功能:操作DateTime的一些静态方法。
  8. /// 作者:彭昭成
  9. /// 时间:2018年12月10日
  10. /// </summary>
  11. public static class DateTimeEx
  12. {
  13. /// <summary>
  14. /// 得到中文形式的日期
  15. /// </summary>
  16. /// <param name="dt"></param>
  17. /// <returns></returns>
  18. public static string GetChineseDate(DateTime dt)
  19. {
  20. return dt.Year.ToString() + "年" + dt.Month.ToString() + "月" + dt.Day.ToString() + "日";
  21. }
  22. /// <summary>
  23. /// 得到中文形式的时间
  24. /// </summary>
  25. /// <param name="dt"></param>
  26. /// <returns></returns>
  27. public static string GetChineseTime(DateTime dt)
  28. {
  29. return dt.Hour.ToString() + "点" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒";
  30. }
  31. /// <summary>
  32. /// 得到中文形式的日期时间
  33. /// </summary>
  34. /// <param name="dt"></param>
  35. /// <returns></returns>
  36. public static string GetChineseDateTime(DateTime dt)
  37. {
  38. return GetChineseDate(dt) + GetChineseTime(dt);
  39. }
  40. /// <summary>
  41. /// 得到某月的最后一天
  42. /// </summary>
  43. /// <param name="dt">日期</param>
  44. /// <returns>返回最后一天的日期</returns>
  45. public static DateTime GetLastDateOfMonth(DateTime dt)
  46. {
  47. return GetLastDateOfMonth(dt.Year, dt.Month);
  48. }
  49. /// <summary>
  50. /// 得到某月的最后一天
  51. /// </summary>
  52. /// <param name="year">年</param>
  53. /// <param name="month">月</param>
  54. /// <returns>返回最后一天的日期</returns>
  55. public static DateTime GetLastDateOfMonth(int year, int month)
  56. {
  57. if (month < 1) month = 1;
  58. if (month > 12) month = 12;
  59. return (new DateTime(year, month, 1)).AddMonths(1).AddDays(-1);
  60. }
  61. /// <summary>
  62. /// 得到某月的最后一豪秒
  63. /// </summary>
  64. /// <param name="dt">日期</param>
  65. /// <returns>返回最后一豪秒的日期</returns>
  66. public static DateTime GetLastDateTimeOfMonth(DateTime dt)
  67. {
  68. return GetLastDateTimeOfMonth(dt.Year, dt.Month);
  69. }
  70. /// <summary>
  71. /// 得到某月的最后一豪秒
  72. /// </summary>
  73. /// <param name="year">年</param>
  74. /// <param name="month">月</param>
  75. /// <returns>返回最后一豪秒的日期</returns>
  76. public static DateTime GetLastDateTimeOfMonth(int year, int month)
  77. {
  78. if (month < 1) month = 1;
  79. if (month > 12) month = 12;
  80. return (new DateTime(year, month, 1)).AddMonths(1).AddSeconds(-1);
  81. }
  82. /// <summary>
  83. /// 得到某月的第一天
  84. /// </summary>
  85. /// <param name="dt">日期</param>
  86. /// <returns>返回第一天的日期</returns>
  87. public static DateTime GetFirstDateOfMonth(DateTime dt)
  88. {
  89. return GetFirstDateOfMonth(dt.Year, dt.Month);
  90. }
  91. /// <summary>
  92. /// 得到某月的第一天
  93. /// </summary>
  94. /// <param name="year">年</param>
  95. /// <param name="month">月</param>
  96. /// <returns>返回第一天的日期</returns>
  97. public static DateTime GetFirstDateOfMonth(int year, int month)
  98. {
  99. if (month < 1) month = 1;
  100. if (month > 12) month = 12;
  101. return new DateTime(year, month, 1);
  102. }
  103. /// <summary>
  104. /// 得到某年的第一天
  105. /// </summary>
  106. /// <param name="year">年</param>
  107. /// <returns>返回第一天的日期</returns>
  108. public static DateTime GetFirstDateOfYear(int year)
  109. {
  110. return new DateTime(year, 1, 1);
  111. }
  112. /// <summary>
  113. /// 得到某年的第一天
  114. /// </summary>
  115. /// <param name="dt">日期</param>
  116. /// <returns>返回第一天的日期</returns>
  117. public static DateTime GetFirstDateOfYear(DateTime dt)
  118. {
  119. return GetFirstDateOfYear(dt.Year);
  120. }
  121. /// <summary>
  122. /// 得到某年的最后一天
  123. /// </summary>
  124. /// <param name="year">年</param>
  125. /// <returns>返回最后一天的日期</returns>
  126. public static DateTime GetLastDateOfYear(int year)
  127. {
  128. return new DateTime(year, 12, 31);
  129. }
  130. /// <summary>
  131. /// 得到某年的最后一天
  132. /// </summary>
  133. /// <param name="dt">日期</param>
  134. /// <returns>返回最后一天的日期</returns>
  135. public static DateTime GetLastDateOfYear(DateTime dt)
  136. {
  137. return GetLastDateOfYear(dt.Year);
  138. }
  139. /// <summary>
  140. /// 得到某年的最后一毫秒
  141. /// </summary>
  142. /// <param name="year">年</param>
  143. /// <returns>返回最后一毫秒的日期时间对象</returns>
  144. public static DateTime GetLastDateTimeOfYear(int year)
  145. {
  146. return new DateTime(year + 1, 1, 1).AddSeconds(-1);
  147. }
  148. /// <summary>
  149. /// 得到某年的最后一毫秒
  150. /// </summary>
  151. /// <param name="dt">日期</param>
  152. /// <returns>返回最后一毫秒的日期时间对象</returns>
  153. public static DateTime GetLastDateTimeOfYear(DateTime dt)
  154. {
  155. return GetLastDateTimeOfYear(dt.Year);
  156. }
  157. /// <summary>
  158. /// 计算某段时间内,每月的天数
  159. /// </summary>
  160. /// <param name="beginTime">开始时间</param>
  161. /// <param name="endTime">结束时间</param>
  162. /// <returns>返回天数</returns>
  163. public static float GetDaysOfMonth(DateTime beginTime, DateTime endTime)
  164. {
  165. float days;
  166. if (beginTime.Year == endTime.Year && beginTime.Month == endTime.Month)
  167. {
  168. //如果是同一个月,返回这个月的天数
  169. DateTime t1 = new DateTime(beginTime.Year, beginTime.Month, 1);
  170. DateTime t2 = t1.AddMonths(1);
  171. TimeSpan interval = t2.Subtract(t1);
  172. days = interval.Days;
  173. }
  174. else if (beginTime.Year == endTime.Year)
  175. {
  176. //如果是同一年,返回这一年的平均月天数
  177. DateTime t1 = new DateTime(beginTime.Year, 1, 1);
  178. DateTime t2 = new DateTime(beginTime.Year + 1, 1, 1);
  179. TimeSpan interval = t2.Subtract(t1);
  180. days = (float)(1.0 * interval.Days / 12);
  181. }
  182. else
  183. {
  184. //否则返回每4年的平均月天数
  185. days = (float)((365.0 * 3 + 366) / 48);
  186. }
  187. return days;
  188. }
  189. /// <summary>
  190. /// 得到某日所在周的第一天
  191. /// </summary>
  192. /// <param name="dt">日期</param>
  193. /// <returns>返回某日所在周的第一天</returns>
  194. public static DateTime GetFirstDateOfWeek(DateTime dt)
  195. {
  196. int days = (int)dt.DayOfWeek;
  197. TimeSpan ts = new TimeSpan(days, 0, 0, 0);
  198. return dt.Date - ts;
  199. }
  200. /// <summary>
  201. /// 得到某日所在周的最后一天
  202. /// </summary>
  203. /// <param name="dt">日期</param>
  204. /// <returns>返回某日所在周的最后一天</returns>
  205. public static DateTime GetLastDateOfWeek(DateTime dt)
  206. {
  207. int days = 6 - (int)dt.DayOfWeek;
  208. TimeSpan ts = new TimeSpan(days, 0, 0, 0);
  209. return dt.Date + ts;
  210. }
  211. /// <summary>
  212. /// 得到某日所在周的最后一豪秒
  213. /// </summary>
  214. /// <param name="dt">日期</param>
  215. /// <returns>返回某日所在周的最后一毫秒</returns>
  216. public static DateTime GetLastDateTimeOfWeek(DateTime dt)
  217. {
  218. int days = 6 - (int)dt.DayOfWeek;
  219. TimeSpan ts = new TimeSpan(days, 0, 0, 0);
  220. DateTime lastDate = dt.Date + ts;
  221. return new DateTime(lastDate.Year, lastDate.Month, lastDate.Day, 23, 59, 59, 59);
  222. }
  223. /// <summary>
  224. /// 得到本月已经过去的天数
  225. /// </summary>
  226. /// <returns>返回本月已经过去的天数</returns>
  227. public static int GetThisMonthPastDays()
  228. {
  229. int days;
  230. DateTime today = DateTime.Today;
  231. DateTime firstDayOfCurMonth = DateTimeEx.GetFirstDateOfMonth(today);
  232. TimeSpan ts = today - firstDayOfCurMonth;
  233. days = (int)ts.TotalDays;
  234. if (days < 1)
  235. days = 1;
  236. return days;
  237. }
  238. /// <summary>
  239. /// 得到本年已经过去的天数
  240. /// </summary>
  241. /// <returns>返回本年已经过去的天数</returns>
  242. public static int GetThisYearPastDays()
  243. {
  244. int days;
  245. DateTime today = DateTime.Today;
  246. DateTime firstDayOfCurYear = DateTimeEx.GetFirstDateOfYear(today);
  247. TimeSpan ts = today - firstDayOfCurYear;
  248. days = (int)ts.TotalDays;
  249. if (days < 1) days = 1;
  250. return days;
  251. }
  252. /// <summary>
  253. /// 得到本周已经过去的天数
  254. /// </summary>
  255. /// <returns>返回本周已经过去的天数</returns>
  256. public static int GetThisWeekPastDays()
  257. {
  258. int days;
  259. DateTime today = DateTime.Today;
  260. DateTime firstDayOfCurWeek = DateTimeEx.GetFirstDateOfWeek(today);
  261. TimeSpan ts = today - firstDayOfCurWeek;
  262. days = (int)ts.TotalDays;
  263. if (days < 1) days = 1;
  264. return days;
  265. }
  266. /// <summary>
  267. /// 得到某日所在季度的第一天
  268. /// </summary>
  269. /// <param name="dt"></param>
  270. /// <returns></returns>
  271. public static DateTime GetFirstDateOfQuarter(DateTime dt)
  272. {
  273. int year = dt.Year;
  274. int month;
  275. int dtMonth = dt.Month;
  276. if (dtMonth == 1 || dtMonth == 2 || dtMonth == 3)
  277. month = 1;
  278. else if (dtMonth == 4 || dtMonth == 5 || dtMonth == 6)
  279. month = 4;
  280. else if (dtMonth == 7 || dtMonth == 8 || dtMonth == 9)
  281. month = 7;
  282. else
  283. month = 10;
  284. return new DateTime(year, month, 1);
  285. }
  286. /// <summary>
  287. /// 得到某日所在季度的最后一天
  288. /// </summary>
  289. /// <param name="dt"></param>
  290. /// <returns></returns>
  291. public static DateTime GetLastDateOfQuarter(DateTime dt)
  292. {
  293. return GetFirstDateOfQuarter(dt).AddMonths(3).AddDays(-1); //即下一季度第1天减1天
  294. }
  295. /// <summary>
  296. /// 得到某日所在季度的最后一天的最后一秒
  297. /// </summary>
  298. /// <param name="dt"></param>
  299. /// <returns></returns>
  300. public static DateTime GetLastDateTimeOfQuarter(DateTime dt)
  301. {
  302. return GetFirstDateOfQuarter(dt).AddMonths(3).AddSeconds(-1); //即下一季度第1天减1秒
  303. }
  304. /// <summary>
  305. /// 转换成中文形式的时间间隔“xx天xx时xx分xx秒”
  306. /// </summary>
  307. /// <param name="ts">时间间隔</param>
  308. /// <param name="showSeconds">是否显示秒</param>
  309. /// <returns>返回中文形式的时间间隔</returns>
  310. public static string GetChineseTimeSpan(TimeSpan ts, bool showSeconds)
  311. {
  312. int days, hours, minutes, seconds;
  313. StringBuilder sbTs = new StringBuilder();
  314. if (ts.Ticks == 0)
  315. {
  316. days = 0;
  317. hours = 0;
  318. minutes = 0;
  319. seconds = 0;
  320. }
  321. else
  322. {
  323. //如果时间间隔为负,将其转成正数
  324. if (ts.Ticks < 0)
  325. {
  326. sbTs.Append("-");
  327. ts = ts.Negate();
  328. }
  329. //计算天、小时、分钟和秒
  330. days = ts.Days;
  331. if (days != 0)
  332. ts = ts.Subtract(new TimeSpan(days, 0, 0, 0));
  333. hours = ts.Hours;
  334. if (hours != 0)
  335. ts = ts.Subtract(new TimeSpan(0, hours, 0, 0));
  336. minutes = ts.Minutes;
  337. if (minutes != 0)
  338. ts = ts.Subtract(new TimeSpan(0, 0, minutes, 0));
  339. seconds = ts.Seconds;
  340. }
  341. sbTs.AppendFormat("{0}天{1}时{2}分{3}", days, hours, minutes, showSeconds ? (seconds.ToString() + "秒") : "");
  342. return sbTs.ToString();
  343. }
  344. /// <summary>
  345. /// 获取日期所在年的周数
  346. /// </summary>
  347. /// <param name="dt"></param>
  348. /// <returns></returns>
  349. public static int GetWeekOfYear(DateTime dt)
  350. {
  351. DateTime yearD = new DateTime(dt.Year, 1, 1);
  352. int diff = Convert.ToInt32(yearD.DayOfWeek);
  353. int dayOfYear = dt.DayOfYear + diff;
  354. return (int)(Math.Ceiling(dayOfYear / 7.0));
  355. }
  356. }
  357. }