using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using Xceed.Wpf.Toolkit; namespace Muchinfo.MTPClient.Infrastructure.Helpers { public static class DateTimeHelper { /// /// Builds the quarter. /// /// The year list. /// System.Collections.Generic.List{System.String}. public static Dictionary BuildQuarter(List yearList) { if (yearList == null) return null; var list = new Dictionary(); var curYear = DateTime.Now.Year.ToString(); var curMonth = DateTime.Now.Month; var curQuarter = 4; if (curMonth < 4) { curQuarter = 1; } else if (curQuarter < 7) { curQuarter = 2; } else if (curQuarter < 10) { curQuarter = 3; } else if (curQuarter < 13) { curQuarter = 4; } foreach (var year in yearList) { for (int i = 1; i < 5; i++) { if (curYear == year && i > curQuarter) break; list.Add(year + i.ToString().PadLeft(2, '0'), year + i.ToString().PadLeft(2, '0')); } } return list; } /// /// Builds the month. /// /// The year list. /// List{System.String}. public static Dictionary BuildMonth(List yearList) { if (yearList == null) return null; var list = new Dictionary(); var curYear = DateTime.Now.Year.ToString(); var curMonth = DateTime.Now.Month; foreach (var year in yearList) { for (int i = 1; i <= 12; i++) { if (curYear == year && i > curMonth) break; list.Add(year + i.ToString().PadLeft(2, '0'), year + i.ToString().PadLeft(2, '0')); } } return list; } /// /// Builds the week. /// /// The year list. /// List{System.String}. public static Dictionary BuildWeek(List yearList) { if (yearList == null) return null; var list = new Dictionary(); var curYear = DateTime.Now.Year.ToString(); var gc = new GregorianCalendar(); var curWeek = gc.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Sunday); foreach (var year in yearList) { for (int i = 1; i < 53; i++) { if (curYear == year && i > curWeek) break; list.Add(year + i.ToString().PadLeft(2, '0'), year + i.ToString().PadLeft(2, '0')); } } return list; } /// /// 获取某一季的开始\结束日期 /// /// The year. /// The quarter. /// KeyValuePair{System.StringSystem.String}. public static KeyValuePair GetQuarterStartEnd(int year, int quarter) { switch (quarter) { case 1: return new KeyValuePair(year + "-01-31", year + "-03-31"); case 2: return new KeyValuePair(year + "-04-30", year + "-06-30"); case 3: return new KeyValuePair(year + "-07-31", year + "-09-30"); case 4: return new KeyValuePair(year + "-10-31", year + "-12-31"); default: return new KeyValuePair(year + "-01-31", year + "-03-31"); } } /// /// 获取某一周的开始\结束日期--网上抄的 /// /// The year. /// The weeks. /// The weekrule. /// if set to true [first day is sunday]. /// The first. /// The last. /// true if XXXX, false otherwise. public static bool GetDaysOfWeeks(int year, int weeks, CalendarWeekRule weekrule, bool firstDayIsSunday, out DateTime first, out DateTime last) { //初始化 out 参数 first = DateTime.MinValue; last = DateTime.MinValue; //不用解释了吧... if (year < 1 | year > 9999) return false; //一年最多53周地球人都知道... if (weeks < 1 | weeks > 53) return false; //取当年首日为基准...为什么?容易得呗... DateTime firstCurr = new DateTime(year, 1, 1); //取下一年首日用于计算... DateTime firstNext = new DateTime(year + 1, 1, 1); //将当年首日星期几转换为数字...星期日特别处理...ISO 8601 标准... int dayOfWeekFirst = (int)firstCurr.DayOfWeek; if (dayOfWeekFirst == 0) dayOfWeekFirst = 7; //得到未经验证的周首日... first = firstCurr.AddDays((weeks - 1) * 7 - dayOfWeekFirst + (firstDayIsSunday ? 0 : 1)); //周首日是上一年日期的情况... if (first.Year < year) { switch (weekrule) { case CalendarWeekRule.FirstDay: //不用解释了吧... first = firstCurr; break; case CalendarWeekRule.FirstFullWeek: //顺延一周... first = first.AddDays(7); break; case CalendarWeekRule.FirstFourDayWeek: //周首日距年首日不足4天则顺延一周... if (firstCurr.Subtract(first).Days > 3) { first = first.AddDays(7); } break; default: break; } } //得到未经验证的周末日... last = first.AddDays(7).AddSeconds(-1); //周末日是下一年日期的情况... if (last.Year > year) { switch (weekrule) { case CalendarWeekRule.FirstDay: last = firstNext.AddSeconds(-1); break; case CalendarWeekRule.FirstFullWeek: //不用处理 break; case CalendarWeekRule.FirstFourDayWeek: //周末日距下一年首日不足4天则提前一周... if (firstNext.Subtract(first).Days < 4) { first = first.AddDays(-7); last = last.AddDays(-7); } break; default: break; } } return true; } } }