/// <summary>Returns a <see langword="Long" /> value specifying the number of time intervals between two <see langword="Date" /> values.</summary> /// <param name="Interval">Required. <see langword="DateInterval" /> enumeration value or <see langword="String" /> expression representing the time interval you want to use as the unit of difference between <paramref name="Date1" /> and <paramref name="Date2" />.</param> /// <param name="Date1">Required. <see langword="Date" />. The first date/time value you want to use in the calculation. </param> /// <param name="Date2">Required. <see langword="Date" />. The second date/time value you want to use in the calculation.</param> /// <param name="DayOfWeek">Optional. A value chosen from the <see langword="FirstDayOfWeek" /> enumeration that specifies the first day of the week. If not specified, <see langword="FirstDayOfWeek.Sunday" /> is used.</param> /// <param name="WeekOfYear">Optional. A value chosen from the <see langword="FirstWeekOfYear" /> enumeration that specifies the first week of the year. If not specified, <see langword="FirstWeekOfYear.Jan1" /> is used.</param> /// <returns>Returns a <see langword="Long" /> value specifying the number of time intervals between two <see langword="Date" /> values.</returns> /// <exception cref="T:System.ArgumentException">Invalid <paramref name="Interval" />.</exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="Date1" />, <paramref name="Date2" />, or <paramref name="DayofWeek" /> is out of range.</exception> /// <exception cref="T:System.InvalidCastException"> /// <paramref name="Date1" /> or <paramref name="Date2" /> is of an invalid type.</exception> public static long DateDiff(DateInterval Interval, DateTime Date1, DateTime Date2, FirstDayOfWeek DayOfWeek = FirstDayOfWeek.Sunday, FirstWeekOfYear WeekOfYear = FirstWeekOfYear.Jan1) { TimeSpan timeSpan = Date2.Subtract(Date1); switch (Interval) { case DateInterval.Year: Calendar currentCalendar1 = DateAndTime.CurrentCalendar; return((long)checked (currentCalendar1.GetYear(Date2) - currentCalendar1.GetYear(Date1))); case DateInterval.Quarter: Calendar currentCalendar2 = DateAndTime.CurrentCalendar; return((long)checked ((currentCalendar2.GetYear(Date2) - currentCalendar2.GetYear(Date1)) * 4 + unchecked (checked (currentCalendar2.GetMonth(Date2) - 1) / 3) - unchecked (checked (currentCalendar2.GetMonth(Date1) - 1) / 3))); case DateInterval.Month: Calendar currentCalendar3 = DateAndTime.CurrentCalendar; return((long)checked ((currentCalendar3.GetYear(Date2) - currentCalendar3.GetYear(Date1)) * 12 + currentCalendar3.GetMonth(Date2) - currentCalendar3.GetMonth(Date1))); case DateInterval.DayOfYear: case DateInterval.Day: return(checked ((long)Math.Round(Conversion.Fix(timeSpan.TotalDays)))); case DateInterval.WeekOfYear: Date1 = Date1.AddDays((double)checked (-DateAndTime.GetDayOfWeek(Date1, DayOfWeek))); Date2 = Date2.AddDays((double)checked (-DateAndTime.GetDayOfWeek(Date2, DayOfWeek))); return(checked ((long)Math.Round(Conversion.Fix(Date2.Subtract(Date1).TotalDays))) / 7L); case DateInterval.Weekday: return(checked ((long)Math.Round(Conversion.Fix(timeSpan.TotalDays))) / 7L); case DateInterval.Hour: return(checked ((long)Math.Round(Conversion.Fix(timeSpan.TotalHours)))); case DateInterval.Minute: return(checked ((long)Math.Round(Conversion.Fix(timeSpan.TotalMinutes)))); case DateInterval.Second: return(checked ((long)Math.Round(Conversion.Fix(timeSpan.TotalSeconds)))); default: throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", new string[1] { nameof(Interval) })); } }
/// <summary>Returns an <see langword="Integer" /> value containing the specified component of a given <see langword="Date" /> value.</summary> /// <param name="Interval">Required. <see langword="DateInterval" /> enumeration value or <see langword="String" /> expression representing the part of the date/time value you want to return.</param> /// <param name="DateValue">Required. <see langword="Date" /> value that you want to evaluate.</param> /// <param name="FirstDayOfWeekValue">Optional. A value chosen from the <see langword="FirstDayOfWeek" /> enumeration that specifies the first day of the week. If not specified, <see langword="FirstDayOfWeek.Sunday" /> is used.</param> /// <param name="FirstWeekOfYearValue">Optional. A value chosen from the <see langword="FirstWeekOfYear" /> enumeration that specifies the first week of the year. If not specified, <see langword="FirstWeekOfYear.Jan1" /> is used.</param> /// <returns>Returns an <see langword="Integer" /> value containing the specified component of a given <see langword="Date" /> value.</returns> /// <exception cref="T:System.ArgumentException"> /// <paramref name="Interval" /> is not valid. </exception> /// <exception cref="T:System.InvalidCastException"> /// <paramref name="DateValue" /> is not coercible to <see langword="Date" />.</exception> public static int DatePart(DateInterval Interval, DateTime DateValue, FirstDayOfWeek FirstDayOfWeekValue = FirstDayOfWeek.Sunday, FirstWeekOfYear FirstWeekOfYearValue = FirstWeekOfYear.Jan1) { switch (Interval) { case DateInterval.Year: return(DateAndTime.CurrentCalendar.GetYear(DateValue)); case DateInterval.Quarter: return(checked (unchecked (checked (DateValue.Month - 1) / 3) + 1)); case DateInterval.Month: return(DateAndTime.CurrentCalendar.GetMonth(DateValue)); case DateInterval.DayOfYear: return(DateAndTime.CurrentCalendar.GetDayOfYear(DateValue)); case DateInterval.Day: return(DateAndTime.CurrentCalendar.GetDayOfMonth(DateValue)); case DateInterval.WeekOfYear: DayOfWeek firstDayOfWeek = FirstDayOfWeekValue != FirstDayOfWeek.System ? (DayOfWeek)(FirstDayOfWeekValue - 1) : Utils.GetCultureInfo().DateTimeFormat.FirstDayOfWeek; CalendarWeekRule rule = CalendarWeekRule.FirstDay; switch (FirstWeekOfYearValue) { case FirstWeekOfYear.System: rule = Utils.GetCultureInfo().DateTimeFormat.CalendarWeekRule; break; case FirstWeekOfYear.Jan1: rule = CalendarWeekRule.FirstDay; break; case FirstWeekOfYear.FirstFourDays: rule = CalendarWeekRule.FirstFourDayWeek; break; case FirstWeekOfYear.FirstFullWeek: rule = CalendarWeekRule.FirstFullWeek; break; } return(DateAndTime.CurrentCalendar.GetWeekOfYear(DateValue, rule, firstDayOfWeek)); case DateInterval.Weekday: return(DateAndTime.Weekday(DateValue, FirstDayOfWeekValue)); case DateInterval.Hour: return(DateAndTime.CurrentCalendar.GetHour(DateValue)); case DateInterval.Minute: return(DateAndTime.CurrentCalendar.GetMinute(DateValue)); case DateInterval.Second: return(DateAndTime.CurrentCalendar.GetSecond(DateValue)); default: throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", new string[1] { nameof(Interval) })); } }