| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- 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
- {
- /// <summary>
- /// Builds the quarter.
- /// </summary>
- /// <param name="yearList">The year list.</param>
- /// <returns>System.Collections.Generic.List{System.String}.</returns>
- public static Dictionary<string, string> BuildQuarter(List<string> yearList)
- {
- if (yearList == null) return null;
- var list = new Dictionary<string, string>();
- 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;
- }
- /// <summary>
- /// Builds the month.
- /// </summary>
- /// <param name="yearList">The year list.</param>
- /// <returns>List{System.String}.</returns>
- public static Dictionary<string, string> BuildMonth(List<string> yearList)
- {
- if (yearList == null) return null;
- var list = new Dictionary<string, string>();
- 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;
- }
- /// <summary>
- /// Builds the week.
- /// </summary>
- /// <param name="yearList">The year list.</param>
- /// <returns>List{System.String}.</returns>
- public static Dictionary<string, string> BuildWeek(List<string> yearList)
- {
- if (yearList == null) return null;
- var list = new Dictionary<string, string>();
- 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;
- }
- /// <summary>
- /// 获取某一季的开始\结束日期
- /// </summary>
- /// <param name="year">The year.</param>
- /// <param name="quarter">The quarter.</param>
- /// <returns>KeyValuePair{System.StringSystem.String}.</returns>
- public static KeyValuePair<string, string> GetQuarterStartEnd(int year, int quarter)
- {
- switch (quarter)
- {
- case 1:
- return new KeyValuePair<string, string>(year + "-01-31", year + "-03-31");
- case 2:
- return new KeyValuePair<string, string>(year + "-04-30", year + "-06-30");
- case 3:
- return new KeyValuePair<string, string>(year + "-07-31", year + "-09-30");
- case 4:
- return new KeyValuePair<string, string>(year + "-10-31", year + "-12-31");
- default:
- return new KeyValuePair<string, string>(year + "-01-31", year + "-03-31");
- }
- }
- /// <summary>
- /// 获取某一周的开始\结束日期--网上抄的
- /// </summary>
- /// <param name="year">The year.</param>
- /// <param name="weeks">The weeks.</param>
- /// <param name="weekrule">The weekrule.</param>
- /// <param name="firstDayIsSunday">if set to <c>true</c> [first day is sunday].</param>
- /// <param name="first">The first.</param>
- /// <param name="last">The last.</param>
- /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
- 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;
- }
- }
- }
|