Turnersville, NJ—Here's a simple method for determining the first and last day of an arbitrary time period. For example, have you ever needed to get the first and last date of "last quarter", or "next month"? This method returns a simple structure with those dates. Simply pass the method a date in time, like today, and the date type you'd like to retrieve.
For example, if you want the first date and last date for next quarter, you'd make a DateTime variable and assign it today's date plus 3 months (to create a date that occurs some time in the next quarter), and then pass that along with DateRangeOptions.Quarter to the method. The method will return a structure with the first and last date of that quarter.
Usage Example: DateRangeStruct retVal;
retVal.startDate = retVal.endDate = Now;
retVal = DateRange(DateRangeOptions.Week, DateTime.Today);
retVal.startDate is the first day of the current week;
retVal.endDate is the last day of the current week.
There is no warranty expressed, or implied, with regard to anything on this site,
much less, the source code below.[Flags]public enum DateRangeOptions : byte{ Week = 1, Month = 2, Quarter = 4, Year = 5}public struct DateRangeStruct{ public DateTime startDate; public DateTime endDate;}/// <summary>/// Returns a string array with start and end dates for a given range./// </summary>/// <param name="range">Enumeration value specifying which abstracted date range to evaluate. Note, weeks begin on Sunday and end on Saturday.</param>/// <param name="relativeDate">Date to use as the basis for calculating the start and end date of the range.</param>/// <returns>DateTimeStruct</returns>public static DateRangeStruct DateRange(DateRangeOptions DRO, DateTime relativeDate){ DateTime[] retValue = { DateTime.Today, DateTime.Today }; DateTime myDate = relativeDate; switch (DRO) { case DateRangeOptions.Week: if(myDate.DayOfWeek > 0) myDate = myDate.AddDays(-1*Convert.ToInt32(myDate.DayOfWeek)); retValue[0] = myDate; retValue[1] = myDate.AddDays(6); break; case DateRangeOptions.Month: if (myDate.Day > 1) myDate = myDate.AddDays(-1 * (myDate.Day - 1)); retValue[0] = myDate; retValue[1] = myDate.AddMonths(1); retValue[1] = retValue[1].AddDays(-1); break; case DateRangeOptions.Quarter: if (myDate.Month < 4) retValue[0] = Convert.ToDateTime("1/1/" + myDate.Year.ToString()); if (myDate.Month > 3 && myDate.Month < 7) retValue[0] = Convert.ToDateTime("4/1/" + myDate.Year.ToString()); if (myDate.Month > 6 && myDate.Month < 10) retValue[0] = Convert.ToDateTime("7/1/" + myDate.Year.ToString()); if (myDate.Month > 9) retValue[0] = Convert.ToDateTime("10/1/" + myDate.Year.ToString()); retValue[1] = retValue[0].AddMonths(3); retValue[1] = retValue[1].AddDays(-1); break; case DateRangeOptions.Year: retValue[0] = Convert.ToDateTime("1/1/" + myDate.Year.ToString()); retValue[1] = Convert.ToDateTime("12/31/" + myDate.Year.ToString()); break; } DateRangeStruct retVal; retVal.startDate = retValue[0]; retVal.endDate = retValue[1]; return retVal;}public static DateRangeStruct DateRange(DateRangeOptions DRO, string relativeDate){ return DateRange(DRO, Convert.ToDateTime(relativeDate));}