/// <summary> /// End of a specific time frame /// </summary> /// <param name="Date">Date to base off of</param> /// <param name="TimeFrame">Time frame to use</param> /// <param name="Culture">Culture to use for calculating (defaults to the current culture)</param> /// <returns>The end of a specific time frame (TimeFrame.Day is the only one that sets the time to 12:59:59 PM, all else are the beginning of the day)</returns> public static DateTime EndOf(this DateTime Date, TimeFrame TimeFrame, CultureInfo Culture = null) { Culture = Culture.Check(CultureInfo.CurrentCulture); if (TimeFrame == TimeFrame.Day) return new DateTime(Date.Year, Date.Month, Date.Day, 23, 59, 59); if (TimeFrame == TimeFrame.Week) return Date.BeginningOf(TimeFrame.Week, Culture).AddDays(6); if (TimeFrame == TimeFrame.Month) return Date.AddMonths(1).BeginningOf(TimeFrame.Month, Culture).AddDays(-1).Date; if (TimeFrame == TimeFrame.Quarter) return Date.EndOf(TimeFrame.Quarter, Date.BeginningOf(TimeFrame.Year, Culture), Culture); return new DateTime(Date.Year, 12, 31); }
/// <summary> /// Gets the number of days left in the time frame specified based on the date /// </summary> /// <param name="Date">Date</param> /// <param name="TimeFrame">Time frame to calculate the number of days left</param> /// <param name="Culture">Culture to use for calculating (defaults to the current culture)</param> /// <returns>The number of days left in the time frame</returns> public static int DaysLeftIn(this DateTime Date, TimeFrame TimeFrame, CultureInfo Culture = null) { Culture = Culture.Check(CultureInfo.CurrentCulture); if (TimeFrame == TimeFrame.Day) return 1; if (TimeFrame == TimeFrame.Week) return 7 - ((int)Date.DayOfWeek + 1); if (TimeFrame == TimeFrame.Month) return Date.DaysIn(TimeFrame.Month, Culture) - Date.Day; if (TimeFrame == TimeFrame.Quarter) return Date.DaysIn(TimeFrame.Quarter, Culture) - (Date.DayOfYear - Date.BeginningOf(TimeFrame.Quarter, Culture).DayOfYear); return Date.DaysIn(TimeFrame.Year, Culture) - Date.DayOfYear; }
/// <summary> /// Beginning of a specific time frame /// </summary> /// <param name="Date">Date to base off of</param> /// <param name="TimeFrame">Time frame to use</param> /// <param name="Culture">Culture to use for calculating (defaults to the current culture)</param> /// <param name="StartOfQuarter1">Start of the first quarter</param> /// <returns>The beginning of a specific time frame</returns> public static DateTime BeginningOf(this DateTime Date, TimeFrame TimeFrame, DateTime StartOfQuarter1, CultureInfo Culture = null) { if (TimeFrame != TimeFrame.Quarter) return Date.BeginningOf(TimeFrame, Culture); Culture = Culture.Check(CultureInfo.CurrentCulture); if (Date.Between(StartOfQuarter1, StartOfQuarter1.AddMonths(3).AddDays(-1).EndOf(TimeFrame.Day, CultureInfo.CurrentCulture))) return StartOfQuarter1.Date; else if (Date.Between(StartOfQuarter1.AddMonths(3), StartOfQuarter1.AddMonths(6).AddDays(-1).EndOf(TimeFrame.Day, CultureInfo.CurrentCulture))) return StartOfQuarter1.AddMonths(3).Date; else if (Date.Between(StartOfQuarter1.AddMonths(6), StartOfQuarter1.AddMonths(9).AddDays(-1).EndOf(TimeFrame.Day, CultureInfo.CurrentCulture))) return StartOfQuarter1.AddMonths(6).Date; return StartOfQuarter1.AddMonths(9).Date; }
/// <summary> /// Gets the number of days in the time frame specified based on the date /// </summary> /// <param name="Date">Date</param> /// <param name="TimeFrame">Time frame to calculate the number of days from</param> /// <param name="Culture">Culture to use for calculating (defaults to the current culture)</param> /// <returns>The number of days in the time frame</returns> public static int DaysIn(this DateTime Date, TimeFrame TimeFrame, CultureInfo Culture = null) { Culture = Culture.Check(CultureInfo.CurrentCulture); if (TimeFrame == TimeFrame.Day) return 1; if (TimeFrame == TimeFrame.Week) return 7; if (TimeFrame == TimeFrame.Month) return Culture.Calendar.GetDaysInMonth(Date.Year, Date.Month); if (TimeFrame == TimeFrame.Quarter) return Date.EndOf(TimeFrame.Quarter, Culture).DayOfYear - Date.BeginningOf(TimeFrame.Quarter, Culture).DayOfYear; return Culture.Calendar.GetDaysInYear(Date.Year); }
/// <summary> /// Beginning of a specific time frame /// </summary> /// <param name="Date">Date to base off of</param> /// <param name="TimeFrame">Time frame to use</param> /// <param name="Culture">Culture to use for calculating (defaults to the current culture)</param> /// <returns>The beginning of a specific time frame</returns> public static DateTime BeginningOf(this DateTime Date, TimeFrame TimeFrame, CultureInfo Culture = null) { Culture = Culture.Check(CultureInfo.CurrentCulture); if (TimeFrame == TimeFrame.Day) return Date.Date; if (TimeFrame == TimeFrame.Week) return Date.AddDays(Culture.DateTimeFormat.FirstDayOfWeek - Date.DayOfWeek).Date; if (TimeFrame == TimeFrame.Month) return new DateTime(Date.Year, Date.Month, 1); if (TimeFrame == TimeFrame.Quarter) return Date.BeginningOf(TimeFrame.Quarter, Date.BeginningOf(TimeFrame.Year, Culture), Culture); return new DateTime(Date.Year, 1, 1); }