/// <summary> /// 根据一年中的第几周获取该周的开始日期与结束日期 /// </summary> /// <param name="year"></param> /// <param name="weekNumber"></param> /// <param name="culture"></param> /// <returns></returns> public static Tuple <DateTime, DateTime> GetFirstEndDayOfWeek(int year, int weekNumber, System.Globalization.CultureInfo culture) { System.Globalization.Calendar calendar = culture.Calendar; DateTime firstOfYear = new DateTime(year, 1, 1, calendar); DateTime targetDay = calendar.AddWeeks(firstOfYear, weekNumber - 1); DayOfWeek firstDayOfWeek = culture.DateTimeFormat.FirstDayOfWeek; while (targetDay.DayOfWeek != firstDayOfWeek) { targetDay = targetDay.AddDays(-1); } return(Tuple.Create <DateTime, DateTime>(targetDay, targetDay.AddDays(6))); }
public static WeekAndDate GetFirstEndDayOfWeekStr(int weekNumber, System.Globalization.CultureInfo culture) { System.Globalization.Calendar calendar = culture.Calendar; DateTime firstOfYear = new DateTime(DateTime.Now.Year, 1, 1, calendar); var index = (int)firstOfYear.DayOfWeek; DateTime targetDay = calendar.AddWeeks(firstOfYear, weekNumber - 1); if (index > 1) { index = index - 1; targetDay = targetDay.AddDays(-index); } //DayOfWeek firstDayOfWeek = culture.DateTimeFormat.FirstDayOfWeek; //while (targetDay.DayOfWeek != firstDayOfWeek) //{ // targetDay = targetDay.AddDays(-1); //} string str = targetDay.ToShortDateString() + "~" + targetDay.AddDays(6).ToShortDateString(); WeekAndDate wad = new WeekAndDate(); wad.Week = weekNumber; wad.BTEDate = str; wad.DateList = new List <DateTime>(); wad.DateList.Add(targetDay); wad.DateList.Add(targetDay.AddDays(1)); wad.DateList.Add(targetDay.AddDays(2)); wad.DateList.Add(targetDay.AddDays(3)); wad.DateList.Add(targetDay.AddDays(4)); return(wad); }
private static IEnumerable<DateTime> GetOccurrences(DateInterval interval, DatePeriod period, Calendar calendar, DateTime start, DateTime end, bool skipWeekends = true) { var difference = DateSpan.GetDifference(interval, start, end)/period.Quantifier; if (start.Kind == DateTimeKind.Utc) { start = start.ToLocalTime(); } for (var i = 0; i < difference; i++) { switch (period.Frequency) { case DatePeriodFrequency.Seconds: var seconds = calendar.AddSeconds(start, period.Quantifier * i); yield return DeferOccurrenceFallingOnWeekend(calendar, seconds, skipWeekends); break; case DatePeriodFrequency.Minutes: var minutes = calendar.AddMinutes(start, period.Quantifier * i); yield return DeferOccurrenceFallingOnWeekend(calendar, minutes, skipWeekends); break; case DatePeriodFrequency.Hours: var hours = calendar.AddHours(start, period.Quantifier * i); yield return DeferOccurrenceFallingOnWeekend(calendar, hours, skipWeekends); break; case DatePeriodFrequency.Days: var days = calendar.AddDays(start, period.Quantifier*i); yield return DeferOccurrenceFallingOnWeekend(calendar, days, skipWeekends); break; case DatePeriodFrequency.Weeks: var weeks = calendar.AddWeeks(start, period.Quantifier*i); yield return DeferOccurrenceFallingOnWeekend(calendar, weeks, skipWeekends); break; case DatePeriodFrequency.Months: var months = calendar.AddMonths(start, period.Quantifier*i); yield return DeferOccurrenceFallingOnWeekend(calendar, months, skipWeekends); break; case DatePeriodFrequency.Years: var years = calendar.AddYears(start, period.Quantifier*i); yield return DeferOccurrenceFallingOnWeekend(calendar, years, skipWeekends); break; default: throw new ArgumentException("Frequency"); } } }
/// <summary> /// Gets the first day of a week. /// </summary> /// <param name="year">The year.</param> /// <param name="week">The week.</param> /// <param name="calendar">The calendar.</param> /// <param name="calendarWeekRule">The calendar week rule.</param> /// <param name="firstDayOfWeek">The first day of week.</param> /// <returns>The date of the first day in the week.</returns> public static DateTime GetFirstDayOfWeek( int year, int week, Calendar calendar = null, CalendarWeekRule calendarWeekRule = CalendarWeekRule.FirstFourDayWeek, DayOfWeek firstDayOfWeek = DayOfWeek.Sunday) { if (calendar == null) calendar = CultureInfo.CurrentCulture.Calendar; DateTime firstDay = new DateTime(year, 1, 7, calendar); while (calendar.GetDayOfWeek(firstDay) != firstDayOfWeek || WeekNumber(firstDay, calendar, calendarWeekRule, firstDayOfWeek) % 52 > 1) { firstDay = calendar.AddDays(firstDay, -1); } // Add the week return (week != 1) ? calendar.AddWeeks(firstDay, week - 1) : firstDay; }