/// <summary> /// Get time rangeTypeName from start of start date of week to start of end date of week /// </summary> /// <param name="currentDate">Current date</param> /// <returns>Time rangeTypeName from start of start date of week to start of end date of week</returns> private static TimeRange _GetWeekRange(DateTime currentDate) { // Get first day of week for current locality DayOfWeek firstDayOfWeekLocality = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek; DayOfWeek dayOfWeek = currentDate.DayOfWeek; int dayDiff = (int)firstDayOfWeekLocality - (int)dayOfWeek; // Get start date for this week DateTime firstDayOfWeek = currentDate.AddDays(dayDiff); // Get end date for this week DateTime lastDayOfWeek = currentDate.AddDays(6 + dayDiff); TimeRange result = new TimeRange(firstDayOfWeek, lastDayOfWeek); return result; }
public static TimeRange GetRange(DateTime currentDate, TimeRanges rangeType) { TimeRange range = null; switch (rangeType) { case TimeRanges.Today: range = new TimeRange(currentDate.Date, currentDate.Date); break; case TimeRanges.Yesterday: DateTime yesterday = currentDate.AddDays(-1); range = new TimeRange(yesterday, yesterday); break; case TimeRanges.ThisWeek: range = _GetWeekRange(currentDate); break; case TimeRanges.LastWeek: TimeRange currentWeekRange = _GetWeekRange(currentDate); range = _GetWeekRange(currentWeekRange.Start.AddDays(-1)); break; case TimeRanges.ThisMonth: range = _GetMonthRange(currentDate); break; case TimeRanges.LastMonth: TimeRange currentMonthRange = _GetMonthRange(currentDate); range = _GetMonthRange(currentMonthRange.Start.AddDays(-1)); break; case TimeRanges.Anyday: range = new TimeRange(DateTime.MinValue.AddYears(1753), DateTime.MaxValue.AddYears(-1000)); break; default: System.Diagnostics.Debug.Assert(false); break; } return range; }
private static TimeRange _GetMonthRange(DateTime currentDate) { DateTime firstDayOfMonth = currentDate.AddDays(1 - currentDate.Day); int dayInMonth = DateTime.DaysInMonth(currentDate.Year, currentDate.Month); DateTime lastDayOfMonth = currentDate.AddDays(dayInMonth - currentDate.Day); TimeRange result = new TimeRange(firstDayOfMonth, lastDayOfMonth); return result; }