DateTimeHelper.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using Xceed.Wpf.Toolkit;
  7. namespace Muchinfo.MTPClient.Infrastructure.Helpers
  8. {
  9. public static class DateTimeHelper
  10. {
  11. /// <summary>
  12. /// Builds the quarter.
  13. /// </summary>
  14. /// <param name="yearList">The year list.</param>
  15. /// <returns>System.Collections.Generic.List{System.String}.</returns>
  16. public static Dictionary<string, string> BuildQuarter(List<string> yearList)
  17. {
  18. if (yearList == null) return null;
  19. var list = new Dictionary<string, string>();
  20. var curYear = DateTime.Now.Year.ToString();
  21. var curMonth = DateTime.Now.Month;
  22. var curQuarter = 4;
  23. if (curMonth < 4)
  24. {
  25. curQuarter = 1;
  26. }
  27. else if (curQuarter < 7)
  28. {
  29. curQuarter = 2;
  30. }
  31. else if (curQuarter < 10)
  32. {
  33. curQuarter = 3;
  34. }
  35. else if (curQuarter < 13)
  36. {
  37. curQuarter = 4;
  38. }
  39. foreach (var year in yearList)
  40. {
  41. for (int i = 1; i < 5; i++)
  42. {
  43. if (curYear == year && i > curQuarter) break;
  44. list.Add(year + i.ToString().PadLeft(2, '0'), year + i.ToString().PadLeft(2, '0'));
  45. }
  46. }
  47. return list;
  48. }
  49. /// <summary>
  50. /// Builds the month.
  51. /// </summary>
  52. /// <param name="yearList">The year list.</param>
  53. /// <returns>List{System.String}.</returns>
  54. public static Dictionary<string, string> BuildMonth(List<string> yearList)
  55. {
  56. if (yearList == null) return null;
  57. var list = new Dictionary<string, string>();
  58. var curYear = DateTime.Now.Year.ToString();
  59. var curMonth = DateTime.Now.Month;
  60. foreach (var year in yearList)
  61. {
  62. for (int i = 1; i <= 12; i++)
  63. {
  64. if (curYear == year && i > curMonth) break;
  65. list.Add(year + i.ToString().PadLeft(2, '0'), year + i.ToString().PadLeft(2, '0'));
  66. }
  67. }
  68. return list;
  69. }
  70. /// <summary>
  71. /// Builds the week.
  72. /// </summary>
  73. /// <param name="yearList">The year list.</param>
  74. /// <returns>List{System.String}.</returns>
  75. public static Dictionary<string, string> BuildWeek(List<string> yearList)
  76. {
  77. if (yearList == null) return null;
  78. var list = new Dictionary<string, string>();
  79. var curYear = DateTime.Now.Year.ToString();
  80. var gc = new GregorianCalendar();
  81. var curWeek = gc.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
  82. foreach (var year in yearList)
  83. {
  84. for (int i = 1; i < 53; i++)
  85. {
  86. if (curYear == year && i > curWeek) break;
  87. list.Add(year + i.ToString().PadLeft(2, '0'), year + i.ToString().PadLeft(2, '0'));
  88. }
  89. }
  90. return list;
  91. }
  92. /// <summary>
  93. /// 获取某一季的开始\结束日期
  94. /// </summary>
  95. /// <param name="year">The year.</param>
  96. /// <param name="quarter">The quarter.</param>
  97. /// <returns>KeyValuePair{System.StringSystem.String}.</returns>
  98. public static KeyValuePair<string, string> GetQuarterStartEnd(int year, int quarter)
  99. {
  100. switch (quarter)
  101. {
  102. case 1:
  103. return new KeyValuePair<string, string>(year + "-01-31", year + "-03-31");
  104. case 2:
  105. return new KeyValuePair<string, string>(year + "-04-30", year + "-06-30");
  106. case 3:
  107. return new KeyValuePair<string, string>(year + "-07-31", year + "-09-30");
  108. case 4:
  109. return new KeyValuePair<string, string>(year + "-10-31", year + "-12-31");
  110. default:
  111. return new KeyValuePair<string, string>(year + "-01-31", year + "-03-31");
  112. }
  113. }
  114. /// <summary>
  115. /// 获取某一周的开始\结束日期--网上抄的
  116. /// </summary>
  117. /// <param name="year">The year.</param>
  118. /// <param name="weeks">The weeks.</param>
  119. /// <param name="weekrule">The weekrule.</param>
  120. /// <param name="firstDayIsSunday">if set to <c>true</c> [first day is sunday].</param>
  121. /// <param name="first">The first.</param>
  122. /// <param name="last">The last.</param>
  123. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  124. public static bool GetDaysOfWeeks(int year, int weeks, CalendarWeekRule weekrule, bool firstDayIsSunday, out DateTime first, out DateTime last)
  125. {
  126. //初始化 out 参数
  127. first = DateTime.MinValue;
  128. last = DateTime.MinValue;
  129. //不用解释了吧...
  130. if (year < 1 | year > 9999)
  131. return false;
  132. //一年最多53周地球人都知道...
  133. if (weeks < 1 | weeks > 53)
  134. return false;
  135. //取当年首日为基准...为什么?容易得呗...
  136. DateTime firstCurr = new DateTime(year, 1, 1);
  137. //取下一年首日用于计算...
  138. DateTime firstNext = new DateTime(year + 1, 1, 1);
  139. //将当年首日星期几转换为数字...星期日特别处理...ISO 8601 标准...
  140. int dayOfWeekFirst = (int)firstCurr.DayOfWeek;
  141. if (dayOfWeekFirst == 0) dayOfWeekFirst = 7;
  142. //得到未经验证的周首日...
  143. first = firstCurr.AddDays((weeks - 1) * 7 - dayOfWeekFirst + (firstDayIsSunday ? 0 : 1));
  144. //周首日是上一年日期的情况...
  145. if (first.Year < year)
  146. {
  147. switch (weekrule)
  148. {
  149. case CalendarWeekRule.FirstDay:
  150. //不用解释了吧...
  151. first = firstCurr;
  152. break;
  153. case CalendarWeekRule.FirstFullWeek:
  154. //顺延一周...
  155. first = first.AddDays(7);
  156. break;
  157. case CalendarWeekRule.FirstFourDayWeek:
  158. //周首日距年首日不足4天则顺延一周...
  159. if (firstCurr.Subtract(first).Days > 3)
  160. {
  161. first = first.AddDays(7);
  162. }
  163. break;
  164. default:
  165. break;
  166. }
  167. }
  168. //得到未经验证的周末日...
  169. last = first.AddDays(7).AddSeconds(-1);
  170. //周末日是下一年日期的情况...
  171. if (last.Year > year)
  172. {
  173. switch (weekrule)
  174. {
  175. case CalendarWeekRule.FirstDay:
  176. last = firstNext.AddSeconds(-1);
  177. break;
  178. case CalendarWeekRule.FirstFullWeek:
  179. //不用处理
  180. break;
  181. case CalendarWeekRule.FirstFourDayWeek:
  182. //周末日距下一年首日不足4天则提前一周...
  183. if (firstNext.Subtract(first).Days < 4)
  184. {
  185. first = first.AddDays(-7);
  186. last = last.AddDays(-7);
  187. }
  188. break;
  189. default:
  190. break;
  191. }
  192. }
  193. return true;
  194. }
  195. }
  196. }