< Summary

Information
Class: MyNet.Temporal.DateTimeExtensions
Assembly: MyNet.Temporal
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Temporal/Extensions/DateTimeExtensions.cs
Tag: 342_31701152804
Line coverage
58%
Covered lines: 134
Uncovered lines: 96
Coverable lines: 230
Total lines: 971
Line coverage: 58.2%
Branch coverage
41%
Covered branches: 52
Total branches: 124
Branch coverage: 41.9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsConsecutiveDays(...)100%210%
ToTimeZone(...)100%11100%
ToLocalTime(...)100%210%
ToUniversalTime(...)100%210%
ToDate(...)100%210%
ToTime(...)100%210%
ToDateTimePeriod(...)100%210%
ToInclusiveDateRange(...)100%11100%
ToDateTimePeriod(...)100%210%
IsBefore(...)100%11100%
IsAfter(...)100%11100%
IsBefore(...)100%210%
IsAfter(...)100%210%
Between(...)0%620%
Between(...)0%620%
Add(...)50%5466.66%
AddCalendarAmount(...)100%11100%
SubtractCalendarAmount(...)100%11100%
EndOfHour(...)100%210%
EndOfDay(...)100%11100%
EndOfDay(...)100%11100%
EndOfWeek(...)100%11100%
EndOfWeek(...)100%210%
EndOfMonth(...)100%11100%
EndOfMonth(...)100%210%
EndOfQuarter(...)100%11100%
EndOfQuarter(...)100%210%
EndOfYear(...)100%11100%
EndOfYear(...)100%210%
EndOfDecade(...)100%11100%
BeginningOfHour(...)100%210%
BeginningOfDay(...)100%11100%
BeginningOfDay(...)100%11100%
BeginningOfWeek(...)100%11100%
BeginningOfWeek(...)100%210%
BeginningOfMonth(...)100%11100%
BeginningOfMonth(...)100%210%
BeginningOfQuarter(...)100%11100%
BeginningOfQuarter(...)100%210%
BeginningOfYear(...)100%11100%
BeginningOfYear(...)100%210%
BeginningOfDecade(...)100%11100%
NextYear(...)100%22100%
PreviousYear(...)100%22100%
NextDay(...)100%11100%
PreviousDay(...)100%11100%
Next(...)100%22100%
Next(...)0%2040%
Previous(...)100%22100%
Previous(...)0%2040%
WeekAfter(...)100%11100%
WeekEarlier(...)100%11100%
IncreaseTime(...)100%210%
DecreaseTime(...)100%210%
At(...)100%210%
At(...)100%210%
At(...)100%11100%
At(...)100%11100%
At(...)100%11100%
SetHour(...)100%11100%
SetMinute(...)100%11100%
SetSecond(...)100%11100%
SetMillisecond(...)100%11100%
Midnight(...)100%11100%
Noon(...)100%11100%
SetDate(...)100%210%
SetDate(...)100%210%
SetDate(...)100%11100%
SetYear(...)100%11100%
SetMonth(...)100%11100%
SetDay(...)100%11100%
FirstDayOfQuarter(...)100%11100%
FirstDayOfMonth(...)100%11100%
LastDayOfQuarter(...)100%11100%
LastDayOfMonth(...)100%11100%
AddBusinessDays(...)100%88100%
SubtractBusinessDays(...)100%11100%
IsInFuture(...)100%11100%
IsInPast(...)100%11100%
Round(...)0%182130%
FirstDayOfWeek(...)75%44100%
FirstDayOfYear(...)100%11100%
LastDayOfWeek(...)100%11100%
LastDayOfYear(...)100%11100%
IsLastDayOfWeek(...)100%210%
IsFirstDayOfWeek(...)100%210%
IsWeekend(...)0%2040%
PreviousMonth(...)100%66100%
NextMonth(...)100%66100%
IsToday(...)0%620%
SameMilliSecond(...)0%620%
SameSecond(...)0%620%
SameMinute(...)0%620%
SameHour(...)0%620%
SameDay(...)100%11100%
SameWeek(...)0%620%
SameMonth(...)100%22100%
SameYear(...)100%11100%
SameDecade(...)100%210%
NumberOfDays(...)100%11100%
CompareWeek(...)100%11100%
NumberOfWeeks(...)100%11100%
CompareMonth(...)100%11100%
NumberOfMonths(...)100%11100%
CompareYear(...)100%11100%
NumberOfYears(...)100%11100%
DiscardDayTime(...)100%210%
DiscardTime(...)100%11100%
InRange(...)83.33%66100%
Normalize(...)50%22100%
Age(...)0%4260%
Range()0%240150%
OrMinValue(...)50%22100%
OrMaxValue(...)0%620%
MinOrNull(...)83.33%6685.71%
MaxOrNull(...)83.33%6685.71%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Temporal/Extensions/DateTimeExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="DateTimeExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Globalization;
 10using System.Linq;
 11using MyNet.Primitives;
 12using MyNet.Temporal.Intervals;
 13
 14#pragma warning disable IDE0130 // Namespace does not match folder structure
 15namespace MyNet.Temporal;
 16#pragma warning restore IDE0130 // Namespace does not match folder structure
 17
 18public static class DateTimeExtensions
 19{
 20    /// <summary>
 21    /// Determines if a collection of DateTime values represents consecutive days. The method first converts the input c
 22    /// </summary>
 23    /// <param name="dates">The collection of DateTime values to check.</param>
 24    /// <returns>True if the dates are consecutive; otherwise, false.</returns>
 25    public static bool IsConsecutiveDays(this IEnumerable<DateTime> dates)
 26    {
 027        var dateList = dates.ToList();
 028        return dateList.Order().Zip(dateList.Order().Skip(1), (a, b) => (a, b)).All(pair => (pair.b - pair.a).TotalDays.
 29    }
 30
 31    extension(DateTime dateTime)
 32    {
 33        /// <summary>
 34        /// Converts the specified <see cref="DateTime"/> to the given time zone.
 35        /// </summary>
 36        /// <param name="timeZoneInfo">The target time zone.</param>
 37        /// <returns>The converted <see cref="DateTime"/> in the specified time zone.</returns>
 1238        public DateTime ToTimeZone(TimeZoneInfo timeZoneInfo) => TimeZoneInfo.ConvertTime(dateTime, timeZoneInfo);
 39
 40        /// <summary>
 41        /// Converts the provided <see cref="DateTime"/> to local time and applies the given time-of-day offset.
 42        /// </summary>
 43        /// <param name="time">The time-of-day to add after converting to local time.</param>
 44        /// <returns>A <see cref="DateTime"/> adjusted to local time and the provided time-of-day.</returns>
 045        public DateTime ToLocalTime(TimeSpan time) => dateTime.ToLocalTime().BeginningOfDay().Add(time);
 46
 47        /// <summary>
 48        /// Converts the provided <see cref="DateTime"/> to local time, applies the given time-of-day and converts to UT
 49        /// </summary>
 50        /// <param name="time">The time-of-day to apply before converting to UTC.</param>
 51        /// <returns>A UTC <see cref="DateTime"/> instance.</returns>
 052        public DateTime ToUniversalTime(TimeSpan time) => dateTime.ToLocalTime().BeginningOfDay().Add(time).ToUniversalT
 53
 54        /// <summary>
 55        /// Returns the date component of the specified <see cref="DateTime"/> as a <see cref="DateOnly"/>.
 56        /// </summary>
 57        /// <returns>A <see cref="DateOnly"/> containing the date part.</returns>
 058        public DateOnly ToDate() => DateOnly.FromDateTime(dateTime);
 59
 60        /// <summary>
 61        /// Returns the time component of the specified <see cref="DateTime"/> as a <see cref="TimeOnly"/>.
 62        /// </summary>
 63        /// <returns>A <see cref="TimeOnly"/> containing the time-of-day part.</returns>
 064        public TimeOnly ToTime() => TimeOnly.FromDateTime(dateTime);
 65
 66        /// <summary>
 67        /// Creates a half-open <see cref="DateTimePeriod"/> <c>[start, end)</c> lasting the specified calendar amount.
 68        /// </summary>
 069        public DateTimePeriod ToDateTimePeriod(CalendarAmount timeSpan) => new(dateTime, dateTime.AddCalendarAmount(time
 70
 71        /// <summary>
 72        /// Creates a closed inclusive <see cref="DateRange"/> of calendar days between this value and <paramref name="e
 73        /// </summary>
 74        public DateRange ToInclusiveDateRange(DateTime end)
 75        {
 376            var (normalizedStart, normalizedEnd) = dateTime.Normalize(end);
 377            return new DateRange(DateOnly.FromDateTime(normalizedStart), DateOnly.FromDateTime(normalizedEnd));
 78        }
 79
 80        /// <summary>
 81        /// Creates a half-open <see cref="DateTimePeriod"/> <c>[min, max)</c> between this value and <paramref name="ot
 82        /// </summary>
 083        public DateTimePeriod ToDateTimePeriod(DateTime otherDateTime) => new(dateTime.Min(otherDateTime), dateTime.Max(
 84
 85        /// <summary>
 86        /// Determines whether the current date is strictly before the specified <see cref="DateTime"/> (converted to a 
 87        /// </summary>
 1888        public bool IsBefore(DateTime toCompareWith) => dateTime < toCompareWith;
 89
 90        /// <summary>
 91        /// Determines whether the current date is strictly after the specified <see cref="DateTime"/> (converted to a <
 92        /// </summary>
 1293        public bool IsAfter(DateTime toCompareWith) => dateTime > toCompareWith;
 94
 95        /// <summary>
 96        /// Determines whether the current date is strictly before the specified <see cref="DateOnly"/>.
 97        /// </summary>
 098        public bool IsBefore(DateOnly toCompareWith) => dateTime.IsBefore(toCompareWith.ToDateTime(TimeOnly.MinValue));
 99
 100        /// <summary>
 101        /// Determines whether the current date is strictly after the specified <see cref="DateOnly"/>.
 102        /// </summary>
 0103        public bool IsAfter(DateOnly toCompareWith) => dateTime.IsAfter(toCompareWith.ToDateTime(TimeOnly.MinValue));
 104
 105        /// <summary>
 106        /// Calculates the absolute difference between this date/time and another date/time, returning the result as a <
 107        /// </summary>
 108        /// <param name="otherDateTime">The other date/time to compare with.</param>
 109        /// <returns>A <see cref="TimeSpan"/> representing the absolute difference between the two dates.</returns>
 0110        public TimeSpan Between(DateTime otherDateTime) => dateTime > otherDateTime ? dateTime - otherDateTime : otherDa
 111
 112        /// <summary>
 113        /// Calculates the absolute difference between this date/time and another date/time, returning the result as a <
 114        /// </summary>
 115        /// <param name="toCompareWith">The other date/time to compare with.</param>
 116        /// <returns>A <see cref="TimeSpan"/> representing the absolute difference between the two dates.</returns>
 0117        public TimeSpan Between(DateOnly toCompareWith) => dateTime > toCompareWith.ToDateTime(TimeOnly.MinValue) ? date
 118
 119        /// <summary>
 120        /// Adds a value expressed in the specified <see cref="TimeUnit"/> to the date.
 121        /// </summary>
 122        /// <param name="value">The amount to add.</param>
 123        /// <param name="timeUnitToGet">The unit of time for the amount.</param>
 124        /// <returns>The adjusted <see cref="DateTime"/>.</returns>
 6125        public DateTime Add(int value, TimeUnit timeUnitToGet) => timeUnitToGet switch
 6126        {
 0127            TimeUnit.Month => dateTime.AddCalendarAmount(value.Months()),
 0128            TimeUnit.Year => dateTime.AddCalendarAmount(value.Years()),
 6129            _ => dateTime.Add(value.ToTimeSpan(timeUnitToGet))
 6130        };
 131
 132        /// <summary>
 133        /// Returns a new <see cref="DateTime"/> that adds the value of the specified <see cref="CalendarAmount"/> to th
 134        /// Calendar components are applied as years, then months, then the fixed <see cref="TimeSpan"/>.
 135        /// </summary>
 136        public DateTime AddCalendarAmount(CalendarAmount timeSpan)
 138137            => dateTime.AddYears(timeSpan.Years)
 138138                .AddMonths(timeSpan.Months)
 138139                .Add(timeSpan.TimeSpan);
 140
 141        /// <summary>
 142        /// Returns a new <see cref="DateTime"/> that subtracts the value of the specified <see cref="CalendarAmount"/> 
 143        /// </summary>
 144        public DateTime SubtractCalendarAmount(CalendarAmount timeSpan)
 120145            => dateTime.AddYears(-timeSpan.Years)
 120146                .AddMonths(-timeSpan.Months)
 120147                .Subtract(timeSpan.TimeSpan);
 148
 149        /// <summary>
 150        /// Returns the very end of the given hour (the last millisecond of the hour for the given <see cref="DateTime"/
 151        /// </summary>
 0152        public DateTime EndOfHour() => new(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, 59, 59, 999, date
 153
 154        /// <summary>
 155        /// Returns the very end of the given day (the last millisecond of the last hour for the given <see cref="DateTi
 156        /// </summary>
 57157        public DateTime EndOfDay() => new(dateTime.Year, dateTime.Month, dateTime.Day, 23, 59, 59, 999, dateTime.Kind);
 158
 159        /// <summary>
 160        /// Returns the timezone-adjusted very end of the given day (the last millisecond of the last hour for the given
 161        /// </summary>
 162        public DateTime EndOfDay(int timeZoneOffset)
 72163            => new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 23, 59, 59, 999, dateTime.Kind)
 72164                .AddHours(timeZoneOffset);
 165
 166        /// <summary>
 167        /// Returns the last day of the week changing the time to the very end of the day. Eg, 2011-12-24T06:40:20.005 =
 168        /// </summary>
 18169        public DateTime EndOfWeek(DayOfWeek? firstDayOfWeek = null) => dateTime.LastDayOfWeek(firstDayOfWeek).EndOfDay()
 170
 171        /// <summary>
 172        /// Returns the last day of the week changing the time to the very end of the day with timezone-adjusted. Eg, 20
 173        /// </summary>
 0174        public DateTime EndOfWeek(int timeZoneOffset, DayOfWeek? firstDayOfWeek = null) => dateTime.LastDayOfWeek(firstD
 175
 176        /// <summary>
 177        /// Returns the last day of the month changing the time to the very end of the day. Eg, 2011-12-24T06:40:20.005 
 178        /// </summary>
 9179        public DateTime EndOfMonth() => dateTime.LastDayOfMonth().EndOfDay();
 180
 181        /// <summary>
 182        /// Returns the last day of the month changing the time to the very end of the day with timezone-adjusted. Eg, 2
 183        /// </summary>
 0184        public DateTime EndOfMonth(int timeZoneOffset) => dateTime.LastDayOfMonth().EndOfDay(timeZoneOffset);
 185
 186        /// <summary>
 187        /// Returns the last day of the quarter changing the time to the very end of the day. Eg, 2011-12-24T06:40:20.00
 188        /// </summary>
 9189        public DateTime EndOfQuarter() => dateTime.LastDayOfQuarter().EndOfDay();
 190
 191        /// <summary>
 192        /// Returns the last day of the quarter changing the time to the very end of the day with timezone-adjusted. Eg,
 193        /// </summary>
 0194        public DateTime EndOfQuarter(int timeZoneOffset) => dateTime.LastDayOfQuarter().EndOfDay(timeZoneOffset);
 195
 196        /// <summary>
 197        /// Returns the last day of the year changing the time to the very end of the day. Eg, 2011-12-24T06:40:20.005 =
 198        /// </summary>
 18199        public DateTime EndOfYear() => dateTime.LastDayOfYear().EndOfDay();
 200
 201        /// <summary>
 202        /// Returns the last day of the year changing the time to the very end of the day with timezone-adjusted. Eg, 20
 203        /// </summary>
 0204        public DateTime EndOfYear(int timeZoneOffset) => dateTime.LastDayOfYear().EndOfDay(timeZoneOffset);
 205
 206        /// <summary>
 207        /// Returns the last day of the year changing the time to the very end of the day. Eg, 2011-12-24T06:40:20.005 =
 208        /// </summary>
 9209        public DateTime EndOfDecade() => dateTime.SetYear(dateTime.Year - (dateTime.Year % 10) + 9).EndOfYear();
 210
 211        /// <summary>
 212        /// Returns the Start of the given day (the first millisecond of the given <see cref="DateTime"/>).
 213        /// </summary>
 0214        public DateTime BeginningOfHour() => new(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, 0, 0, 0, da
 215
 216        /// <summary>
 217        /// Returns the Start of the given day (the first millisecond of the given <see cref="DateTime"/>).
 218        /// </summary>
 108219        public DateTime BeginningOfDay() => new(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, 0, dateTime.Kind);
 220
 221        /// <summary>
 222        /// Returns the timezone-adjusted Start of the given day (the first millisecond of the given <see cref="DateTime
 223        /// </summary>
 224        public DateTime BeginningOfDay(int timezoneOffset)
 72225            => new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, 0, dateTime.Kind)
 72226                .AddHours(timezoneOffset);
 227
 228        /// <summary>
 229        /// Returns the Start day of the week changing the time to the very start of the day. Eg, 2011-12-24T06:40:20.00
 230        /// </summary>
 18231        public DateTime BeginningOfWeek(DayOfWeek? firstDayOfWeek = null) => dateTime.FirstDayOfWeek(firstDayOfWeek).Beg
 232
 233        /// <summary>
 234        /// Returns the Start day of the week changing the time to the very start of the day with timezone-adjusted. Eg,
 235        /// </summary>
 0236        public DateTime BeginningOfWeek(int timezoneOffset, DayOfWeek? firstDayOfWeek = null) => dateTime.FirstDayOfWeek
 237
 238        /// <summary>
 239        /// Returns the Start day of the month changing the time to the very start of the day. Eg, 2011-12-24T06:40:20.0
 240        /// </summary>
 9241        public DateTime BeginningOfMonth() => dateTime.FirstDayOfMonth().BeginningOfDay();
 242
 243        /// <summary>
 244        /// Returns the Start day of the month changing the time to the very start of the day with timezone-adjusted. Eg
 245        /// </summary>
 0246        public DateTime BeginningOfMonth(int timezoneOffset) => dateTime.FirstDayOfMonth().BeginningOfDay(timezoneOffset
 247
 248        /// <summary>
 249        /// Returns the Start day of the quarter changing the time to the very start of the day. Eg, 2011-12-24T06:40:20
 250        /// </summary>
 9251        public DateTime BeginningOfQuarter() => dateTime.FirstDayOfQuarter().BeginningOfDay();
 252
 253        /// <summary>
 254        /// Returns the Start day of the quarter changing the time to the very start of the day with timezone-adjusted. 
 255        /// </summary>
 0256        public DateTime BeginningOfQuarter(int timezoneOffset) => dateTime.FirstDayOfQuarter().BeginningOfDay(timezoneOf
 257
 258        /// <summary>
 259        /// Returns the Start day of the year changing the time to the very start of the day. Eg, 2011-12-24T06:40:20.00
 260        /// </summary>
 18261        public DateTime BeginningOfYear() => dateTime.FirstDayOfYear().BeginningOfDay();
 262
 263        /// <summary>
 264        /// Returns the Start day of the year changing the time to the very start of the day with timezone-adjusted. Eg,
 265        /// </summary>
 0266        public DateTime BeginningOfYear(int timezoneOffset) => dateTime.FirstDayOfYear().BeginningOfDay(timezoneOffset);
 267
 268        /// <summary>
 269        /// Returns the Start day of the year changing the time to the very start of the day with timezone-adjusted. Eg,
 270        /// </summary>
 9271        public DateTime BeginningOfDecade() => dateTime.SetYear(dateTime.Year - (dateTime.Year % 10)).BeginningOfYear();
 272
 273        /// <summary>
 274        /// Returns the same date (same Day, Month, Hour, Minute, Second etc.) in the next calendar year.
 275        /// If that day does not exist in next year in same month, number of missing days is added to the last day in sa
 276        /// </summary>
 277        public DateTime NextYear()
 278        {
 6279            var nextYear = dateTime.Year + 1;
 6280            var numberOfDaysInSameMonthNextYear = DateTime.DaysInMonth(nextYear, dateTime.Month);
 281
 6282            if (numberOfDaysInSameMonthNextYear >= dateTime.Day)
 3283                return new DateTime(nextYear, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Sec
 284
 3285            var differenceInDays = dateTime.Day - numberOfDaysInSameMonthNextYear;
 3286            var dateTime1 = new DateTime(nextYear, dateTime.Month, numberOfDaysInSameMonthNextYear, dateTime.Hour, dateT
 3287            return dateTime1.AddCalendarAmount(differenceInDays.Days());
 288        }
 289
 290        /// <summary>
 291        /// Returns the same date (same Day, Month, Hour, Minute, Second etc.) in the previous calendar year.
 292        /// If that day does not exist in previous year in same month, number of missing days is added to the last day i
 293        /// </summary>
 294        public DateTime PreviousYear()
 295        {
 6296            var previousYear = dateTime.Year - 1;
 6297            var numberOfDaysInSameMonthPreviousYear = DateTime.DaysInMonth(previousYear, dateTime.Month);
 298
 6299            if (numberOfDaysInSameMonthPreviousYear >= dateTime.Day)
 3300                return new DateTime(previousYear, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime
 301
 3302            var differenceInDays = dateTime.Day - numberOfDaysInSameMonthPreviousYear;
 3303            var dateTime1 = new DateTime(previousYear, dateTime.Month, numberOfDaysInSameMonthPreviousYear, dateTime.Hou
 3304            return dateTime1.AddCalendarAmount(differenceInDays.Days());
 305        }
 306
 307        /// <summary>
 308        /// Returns <see cref="DateTime"/> increased by 24 hours ie Next Day.
 309        /// </summary>
 45310        public DateTime NextDay() => dateTime.AddCalendarAmount(1.Days());
 311
 312        /// <summary>
 313        /// Returns <see cref="DateTime"/> decreased by 24h period ie Previous Day.
 314        /// </summary>
 45315        public DateTime PreviousDay() => dateTime.SubtractCalendarAmount(1.Days());
 316
 317        /// <summary>
 318        /// Returns first next occurrence of specified <see cref="DayOfWeek"/>.
 319        /// </summary>
 320        public DateTime Next(DayOfWeek day)
 321        {
 322            do
 323            {
 42324                dateTime = dateTime.NextDay();
 325            }
 42326            while (dateTime.DayOfWeek != day);
 327
 6328            return dateTime;
 329        }
 330
 331        /// <summary>
 332        /// Returns first next occurrence of specified <see cref="DayOfWeek"/>.
 333        /// </summary>
 334        public DateTime Next(int day, int month)
 335        {
 336            do
 337            {
 0338                dateTime = dateTime.NextDay();
 339            }
 0340            while (dateTime.Month != month || dateTime.Day != day);
 341
 0342            return dateTime;
 343        }
 344
 345        /// <summary>
 346        /// Returns first next occurrence of specified <see cref="DayOfWeek"/>.
 347        /// </summary>
 348        public DateTime Previous(DayOfWeek day)
 349        {
 350            do
 351            {
 42352                dateTime = dateTime.PreviousDay();
 353            }
 42354            while (dateTime.DayOfWeek != day);
 355
 6356            return dateTime;
 357        }
 358
 359        /// <summary>
 360        /// Returns first next occurrence of specified <see cref="DayOfWeek"/>.
 361        /// </summary>
 362        public DateTime Previous(int day, int month)
 363        {
 364            do
 365            {
 0366                dateTime = dateTime.PreviousDay();
 367            }
 0368            while (dateTime.Month != month || dateTime.Day != day);
 369
 0370            return dateTime;
 371        }
 372
 373        /// <summary>
 374        /// Increases supplied <see cref="DateTime"/> for 7 days ie returns the Next Week.
 375        /// </summary>
 3376        public DateTime WeekAfter() => dateTime.AddCalendarAmount(1.Weeks());
 377
 378        /// <summary>
 379        /// Decreases supplied <see cref="DateTime"/> for 7 days ie returns the Previous Week.
 380        /// </summary>
 3381        public DateTime WeekEarlier() => dateTime.SubtractCalendarAmount(1.Weeks());
 382
 383        /// <summary>
 384        /// Increases the <see cref="DateTime"/> object with given <see cref="TimeSpan"/> value.
 385        /// </summary>
 0386        public DateTime IncreaseTime(TimeSpan toAdd) => dateTime + toAdd;
 387
 388        /// <summary>
 389        /// Decreases the <see cref="DateTime"/> object with given <see cref="TimeSpan"/> value.
 390        /// </summary>
 0391        public DateTime DecreaseTime(TimeSpan toSubtract) => dateTime - toSubtract;
 392
 393        /// <summary>
 394        /// Returns the original <see cref="DateTime"/> with Hour part changed to supplied hour parameter.
 395        /// </summary>
 0396        public DateTime At(TimeOnly time) => new(dateTime.Year, dateTime.Month, dateTime.Day, time.Hour, time.Minute, ti
 397
 398        /// <summary>
 399        /// Returns the original <see cref="DateTime"/> with Hour part changed to supplied hour parameter.
 400        /// </summary>
 0401        public DateTime At(TimeSpan time) => new(dateTime.Year, dateTime.Month, dateTime.Day, time.Hours, time.Minutes, 
 402
 403        /// <summary>
 404        /// Returns the original <see cref="DateTime"/> with Hour and Minute parts changed to supplied hour and minute p
 405        /// </summary>
 18406        public DateTime At(int hour, int minute) => new(dateTime.Year, dateTime.Month, dateTime.Day, hour, minute, 0, 0,
 407
 408        /// <summary>
 409        /// Returns the original <see cref="DateTime"/> with Hour, Minute and Second parts changed to supplied hour, min
 410        /// </summary>
 18411        public DateTime At(int hour, int minute, int second) => new(dateTime.Year, dateTime.Month, dateTime.Day, hour, m
 412
 413        /// <summary>
 414        /// Returns the original <see cref="DateTime"/> with Hour, Minute, Second and Millisecond parts changed to suppl
 415        /// </summary>
 21416        public DateTime At(int hour, int minute, int second, int millisecond) => new(dateTime.Year, dateTime.Month, date
 417
 418        /// <summary>
 419        /// Returns <see cref="DateTime"/> with changed Hour part.
 420        /// </summary>
 87421        public DateTime SetHour(int hour) => new(dateTime.Year, dateTime.Month, dateTime.Day, hour, dateTime.Minute, dat
 422
 423        /// <summary>
 424        /// Returns <see cref="DateTime"/> with changed Minute part.
 425        /// </summary>
 72426        public DateTime SetMinute(int minute) => new(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, minute,
 427
 428        /// <summary>
 429        /// Returns <see cref="DateTime"/> with changed Second part.
 430        /// </summary>
 72431        public DateTime SetSecond(int second) => new(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTim
 432
 433        /// <summary>
 434        /// Returns <see cref="DateTime"/> with changed Millisecond part.
 435        /// </summary>
 72436        public DateTime SetMillisecond(int millisecond) => new(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hou
 437
 438        /// <summary>
 439        /// Returns original <see cref="DateTime"/> value with time part set to midnight (alias for <see cref="Beginning
 440        /// </summary>
 3441        public DateTime Midnight() => dateTime.BeginningOfDay();
 442
 443        /// <summary>
 444        /// Returns original <see cref="DateTime"/> value with time part set to Noon (12:00:00h).
 445        /// </summary>
 446        /// <returns>A <see cref="DateTime"/> value with time part set to Noon (12:00:00h).</returns>
 3447        public DateTime Noon() => dateTime.At(12, 0, 0, 0);
 448
 449        /// <summary>
 450        /// Returns <see cref="DateTime"/> with changed Year part.
 451        /// </summary>
 0452        public DateTime SetDate(int year) => new(year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dat
 453
 454        /// <summary>
 455        /// Returns <see cref="DateTime"/> with changed Year and Month part.
 456        /// </summary>
 0457        public DateTime SetDate(int year, int month) => new(year, month, dateTime.Day, dateTime.Hour, dateTime.Minute, d
 458
 459        /// <summary>
 460        /// Returns <see cref="DateTime"/> with changed Year, Month and Day part.
 461        /// </summary>
 108462        public DateTime SetDate(int year, int month, int day) => new(year, month, day, dateTime.Hour, dateTime.Minute, d
 463
 464        /// <summary>
 465        /// Returns <see cref="DateTime"/> with changed Year part.
 466        /// </summary>
 18467        public DateTime SetYear(int year) => new(year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dat
 468
 469        /// <summary>
 470        /// Returns <see cref="DateTime"/> with changed Month part.
 471        /// </summary>
 21472        public DateTime SetMonth(int month) => new(dateTime.Year, month, dateTime.Day, dateTime.Hour, dateTime.Minute, d
 473
 474        /// <summary>
 475        /// Returns <see cref="DateTime"/> with changed Day part.
 476        /// </summary>
 69477        public DateTime SetDay(int day) => new(dateTime.Year, dateTime.Month, day, dateTime.Hour, dateTime.Minute, dateT
 478
 479        /// <summary>
 480        /// Sets the day of the <see cref="DateTime"/> to the first day in that calendar quarter.
 481        /// credit to http://www.devcurry.com/2009/05/find-first-and-last-day-of-current.html.
 482        /// </summary>
 483        /// <returns>given <see cref="DateTime"/> with the day part set to the first day in the quarter.</returns>
 484        public DateTime FirstDayOfQuarter()
 485        {
 21486            var currentQuarter = ((dateTime.Month - 1) / 3) + 1;
 21487            var firstDay = new DateTime(dateTime.Year, (3 * currentQuarter) - 2, 1, 0, 0, 0, dateTime.Kind);
 488
 21489            return dateTime.SetDate(firstDay.Year, firstDay.Month, firstDay.Day);
 490        }
 491
 492        /// <summary>
 493        /// Sets the day of the <see cref="DateTime"/> to the first day in that month.
 494        /// </summary>
 495        /// <returns>given <see cref="DateTime"/> with the day part set to the first day in that month.</returns>
 12496        public DateTime FirstDayOfMonth() => dateTime.SetDay(1);
 497
 498        /// <summary>
 499        /// Sets the day of the <see cref="DateTime"/> to the last day in that calendar quarter.
 500        /// credit to http://www.devcurry.com/2009/05/find-first-and-last-day-of-current.html.
 501        /// </summary>
 502        /// <returns>given <see cref="DateTime"/> with the day part set to the last day in the quarter.</returns>
 503        public DateTime LastDayOfQuarter()
 504        {
 21505            var currentQuarter = ((dateTime.Month - 1) / 3) + 1;
 21506            var firstDay = dateTime.SetDate(dateTime.Year, (3 * currentQuarter) - 2, 1);
 21507            return firstDay.SetMonth(firstDay.Month + 2).LastDayOfMonth();
 508        }
 509
 510        /// <summary>
 511        /// Sets the day of the <see cref="DateTime"/> to the last day in that month.
 512        /// </summary>
 513        /// <returns>given <see cref="DateTime"/> with the day part set to the last day in that month.</returns>
 45514        public DateTime LastDayOfMonth() => dateTime.SetDay(DateTime.DaysInMonth(dateTime.Year, dateTime.Month));
 515
 516        /// <summary>
 517        /// Adds the given number of business days to the <see cref="DateTime"/>.
 518        /// </summary>
 519        /// <param name="days">Number of business days to be added.</param>
 520        /// <returns>A <see cref="DateTime"/> increased by a given number of business days.</returns>
 521        public DateTime AddBusinessDays(int days)
 522        {
 12523            var sign = Math.Sign(days);
 12524            var unsignedDays = Math.Abs(days);
 72525            for (var i = 0; i < unsignedDays; i++)
 526            {
 527                do
 528                {
 48529                    dateTime = dateTime.AddDays(sign);
 530                }
 48531                while (dateTime.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday);
 532            }
 533
 12534            return dateTime;
 535        }
 536
 537        /// <summary>
 538        /// Subtracts the given number of business days to the <see cref="DateTime"/>.
 539        /// </summary>
 540        /// <param name="days">Number of business days to be subtracted.</param>
 541        /// <returns>A <see cref="DateTime"/> increased by a given number of business days.</returns>
 6542        public DateTime SubtractBusinessDays(int days) => dateTime.AddBusinessDays(-days);
 543
 544        /// <summary>
 545        /// Determine if a <see cref="DateTime"/> is in the future.
 546        /// </summary>
 547        /// <returns><c>true</c> if <paramref name="dateTime"/> is in the future; otherwise <c>false</c>.</returns>
 9548        public bool IsInFuture() => dateTime.ToUniversalTime() > DateTime.UtcNow;
 549
 550        /// <summary>
 551        /// Determine if a <see cref="DateTime"/> is in the past.
 552        /// </summary>
 553        /// <returns><c>true</c> if <paramref name="dateTime"/> is in the past; otherwise <c>false</c>.</returns>
 6554        public bool IsInPast() => dateTime.ToUniversalTime() < DateTime.UtcNow;
 555
 556        /// <summary>
 557        /// Rounds <paramref name="dateTime"/> to the nearest <see cref="RoundTo"/>.
 558        /// </summary>
 559        /// <returns>The rounded <see cref="DateTime"/>.</returns>
 560        public DateTime Round(RoundTo rt)
 561        {
 562            DateTime rounded;
 563
 564            switch (rt)
 565            {
 566                case RoundTo.Second:
 567                    {
 0568                        rounded = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minu
 0569                        if (dateTime.Millisecond >= 500)
 570                        {
 0571                            rounded = rounded.AddSeconds(1);
 572                        }
 573
 0574                        break;
 575                    }
 576
 577                case RoundTo.Minute:
 578                    {
 0579                        rounded = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minu
 0580                        if (dateTime.Second >= 30)
 581                        {
 0582                            rounded = rounded.AddMinutes(1);
 583                        }
 584
 0585                        break;
 586                    }
 587
 588                case RoundTo.Hour:
 589                    {
 0590                        rounded = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, 0, 0, dateTim
 0591                        if (dateTime.Minute >= 30)
 592                        {
 0593                            rounded = rounded.AddHours(1);
 594                        }
 595
 0596                        break;
 597                    }
 598
 599                case RoundTo.Day:
 600                    {
 0601                        rounded = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, dateTime.Kind);
 0602                        if (dateTime.Hour >= 12)
 603                        {
 0604                            rounded = rounded.AddDays(1);
 605                        }
 606
 0607                        break;
 608                    }
 609
 610                default:
 611                    {
 0612                        throw new ArgumentOutOfRangeException(nameof(rt));
 613                    }
 614            }
 615
 0616            return rounded;
 617        }
 618
 619        /// <summary>
 620        /// Returns a DateTime adjusted to the beginning of the week.
 621        /// </summary>
 622        /// <returns>A DateTime instance adjusted to the beginning of the current week.</returns>
 623        /// <remarks>the beginning of the week is controlled by the current Culture.</remarks>
 624        public DateTime FirstDayOfWeek(DayOfWeek? firstDayOfWeek = null)
 625        {
 87626            var currentCulture = CultureInfo.CurrentCulture;
 87627            var firstDay = firstDayOfWeek ?? currentCulture.DateTimeFormat.FirstDayOfWeek;
 87628            var offset = dateTime.DayOfWeek < firstDay ? 7 : 0;
 87629            var numberOfDaysSinceBeginningOfTheWeek = dateTime.DayOfWeek + offset - firstDay;
 630
 87631            return dateTime.AddDays(-numberOfDaysSinceBeginningOfTheWeek);
 632        }
 633
 634        /// <summary>
 635        /// Returns the first day of the year keeping the time component intact. Eg, 2011-02-04T06:40:20.005 => 2011-01-
 636        /// </summary>
 27637        public DateTime FirstDayOfYear() => dateTime.SetDate(dateTime.Year, 1, 1);
 638
 639        /// <summary>
 640        /// Returns the last day of the week keeping the time component intact. Eg, 2011-12-24T06:40:20.005 => 2011-12-2
 641        /// </summary>
 27642        public DateTime LastDayOfWeek(DayOfWeek? firstDayOfWeek = null) => dateTime.FirstDayOfWeek(firstDayOfWeek).AddDa
 643
 644        /// <summary>
 645        /// Returns the last day of the year keeping the time component intact. Eg, 2011-12-24T06:40:20.005 => 2011-12-3
 646        /// </summary>
 27647        public DateTime LastDayOfYear() => dateTime.SetDate(dateTime.Year, 12, 31);
 648
 649        /// <summary>
 650        /// Determines whether the <see cref="DateTime"/> is the last day of the week. The beginning of the week is dete
 651        /// </summary>
 652        /// <param name="firstDayOfWeek">The first day of the week. If null, the current culture's first day of the week
 653        /// <returns><c>true</c> if the <see cref="DateTime"/> is the last day of the week; otherwise, <c>false</c>.</re
 0654        public bool IsLastDayOfWeek(DayOfWeek? firstDayOfWeek = null) => dateTime.DayOfWeek == dateTime.LastDayOfWeek(fi
 655
 656        /// <summary>
 657        /// Determines whether the <see cref="DateTime"/> is the first day of the week. The beginning of the week is det
 658        /// </summary>
 659        /// <param name="firstDayOfWeek">The first day of the week. If null, the current culture's first day of the week
 660        /// <returns><c>true</c> if the <see cref="DateTime"/> is the first day of the week; otherwise, <c>false</c>.</r
 0661        public bool IsFirstDayOfWeek(DayOfWeek? firstDayOfWeek = null) => dateTime.DayOfWeek == dateTime.FirstDayOfWeek(
 662
 663        /// <summary>
 664        /// Determines whether the <see cref="DateTime"/> falls on a weekend day (Saturday or Sunday).
 665        /// </summary>
 666        /// <returns><c>true</c> if the <see cref="DateTime"/> is a weekend day; otherwise, <c>false</c>.</returns>
 0667        public bool IsWeekend() => dateTime.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday;
 668
 669        /// <summary>
 670        /// Returns the previous month keeping the time component intact. Eg, 2010-01-20T06:40:20.005 => 2009-12-20T06:4
 671        /// If the previous month doesn't have that many days the last day of the previous month is used. Eg, 2009-03-31
 672        /// </summary>
 673        public DateTime PreviousMonth()
 674        {
 6675            var year = dateTime.Month == 1 ? dateTime.Year - 1 : dateTime.Year;
 676
 6677            var month = dateTime.Month == 1 ? 12 : dateTime.Month - 1;
 678
 6679            var firstDayOfPreviousMonth = dateTime.SetDate(year, month, 1);
 680
 6681            var lastDayOfPreviousMonth = firstDayOfPreviousMonth.LastDayOfMonth().Day;
 682
 6683            var day = dateTime.Day > lastDayOfPreviousMonth ? lastDayOfPreviousMonth : dateTime.Day;
 684
 6685            return firstDayOfPreviousMonth.SetDay(day);
 686        }
 687
 688        /// <summary>
 689        /// Returns the next month keeping the time component intact. Eg, 2012-12-05T06:40:20.005 => 2013-01-05T06:40:20
 690        /// If the next month doesn't have that many days the last day of the next month is used. Eg, 2013-01-31T06:40:2
 691        /// </summary>
 692        public DateTime NextMonth()
 693        {
 6694            var year = dateTime.Month == 12 ? dateTime.Year + 1 : dateTime.Year;
 695
 6696            var month = dateTime.Month == 12 ? 1 : dateTime.Month + 1;
 697
 6698            var firstDayOfNextMonth = dateTime.SetDate(year, month, 1);
 699
 6700            var lastDayOfPreviousMonth = firstDayOfNextMonth.LastDayOfMonth().Day;
 701
 6702            var day = dateTime.Day > lastDayOfPreviousMonth ? lastDayOfPreviousMonth : dateTime.Day;
 703
 6704            return firstDayOfNextMonth.SetDay(day);
 705        }
 706
 707        /// <summary>
 708        /// Determines whether the specified <see cref="DateTime"/> value is in the same day (day + month + year) as cur
 709        /// </summary>
 710        public bool IsToday()
 0711            => dateTime.Kind switch
 0712            {
 0713                DateTimeKind.Local => dateTime.SameDay(DateTime.Now),
 0714                _ => dateTime.SameDay(DateTime.UtcNow)
 0715            };
 716
 717        /// <summary>
 718        /// Determines whether the specified <see cref="DateTime"/> value is exactly the same millisecond (millisecond +
 719        /// </summary>
 720        /// <param name="date">The date to compare with.</param>
 721        /// <returns><c>true</c> if the specified date is exactly the same millisecond as the current date; otherwise, <
 0722        public bool SameMilliSecond(DateTime date) => dateTime.SameSecond(date) && dateTime.Millisecond == date.Millisec
 723
 724        /// <summary>
 725        /// Determines whether the specified <see cref="DateTime"/> value is exactly the same second (second + minute + 
 726        /// </summary>
 727        /// <param name="date">The date to compare with.</param>
 728        /// <returns><c>true</c> if the specified date is exactly the same second as the current date; otherwise, <c>fal
 0729        public bool SameSecond(DateTime date) => dateTime.SameMinute(date) && dateTime.Second == date.Second;
 730
 731        /// <summary>
 732        /// Determines whether the specified <see cref="DateTime"/> value is exactly the same minute (minute + hour + da
 733        /// </summary>
 734        /// <param name="date">The date to compare with.</param>
 735        /// <returns><c>true</c> if the specified date is exactly the same minute as the current date; otherwise, <c>fal
 0736        public bool SameMinute(DateTime date) => dateTime.SameHour(date) && dateTime.Minute == date.Minute;
 737
 738        /// <summary>
 739        /// Determines whether the specified <see cref="DateTime"/> value is exactly the same hour (hour + day + month +
 740        /// </summary>
 741        /// <param name="date">The date to compare with.</param>
 742        /// <returns><c>true</c> if the specified date is exactly the same hour as the current date; otherwise, <c>false
 0743        public bool SameHour(DateTime date) => dateTime.SameDay(date) && dateTime.Hour == date.Hour;
 744
 745        /// <summary>
 746        /// Determines whether the specified <see cref="DateTime"/> value is exactly the same day (day + month + year) t
 747        /// </summary>
 748        /// <param name="date">Value to compare with.</param>
 749        /// <returns>
 750        ///     <c>true</c> if the specified date is exactly the same year then current; otherwise, <c>false</c>.
 751        /// </returns>
 51752        public bool SameDay(DateTime date) => dateTime.Date == date.Date;
 753
 754        /// <summary>
 755        /// Determines whether the specified <see cref="DateTime"/> value is exactly the same month (month + year) then 
 756        /// </summary>
 757        /// <param name="date">Value to compare with.</param>
 758        /// <returns>
 759        ///     <c>true</c> if the specified date is exactly the same month and year then current; otherwise, <c>false</
 760        /// </returns>
 0761        public bool SameWeek(DateTime date) => date.IsAfter(dateTime.BeginningOfWeek()) && date < dateTime.EndOfWeek();
 762
 763        /// <summary>
 764        /// Determines whether the specified <see cref="DateTime"/> value is exactly the same month (month + year) then 
 765        /// </summary>
 766        /// <param name="date">Value to compare with.</param>
 767        /// <returns>
 768        ///     <c>true</c> if the specified date is exactly the same month and year then current; otherwise, <c>false</
 769        /// </returns>
 18770        public bool SameMonth(DateTime date) => dateTime.Month == date.Month && dateTime.Year == date.Year;
 771
 772        /// <summary>
 773        /// Determines whether the specified <see cref="DateTime"/> value is exactly the same year then current. Eg, 201
 774        /// </summary>
 775        /// <param name="date">Value to compare with.</param>
 776        /// <returns>
 777        ///     <c>true</c> if the specified date is exactly the same date then current; otherwise, <c>false</c>.
 778        /// </returns>
 18779        public bool SameYear(DateTime date) => dateTime.Year == date.Year;
 780
 781        /// <summary>
 782        /// Determines whether the specified <see cref="DateTime"/> value is exactly the same year then current. Eg, 201
 783        /// </summary>
 784        /// <param name="date">Value to compare with.</param>
 785        /// <returns>
 786        ///     <c>true</c> if the specified date is exactly the same date then current; otherwise, <c>false</c>.
 787        /// </returns>
 0788        public bool SameDecade(DateTime date) => dateTime.BeginningOfDecade() == date.BeginningOfDecade();
 789
 790        /// <summary>
 791        /// Compare two dates and return difference of days (positive or negative).
 792        /// </summary>
 793        /// <param name="dateTo">The date to compare with.</param>
 794        /// <returns>The difference in days between the current date and the specified date.</returns>
 9795        public int NumberOfDays(DateTime dateTo) => Math.Abs((dateTime - dateTo).Days);
 796
 797        /// <summary>
 798        /// Compare two dates and return difference of week (positive or negative).
 799        /// </summary>
 800        /// <param name="dateTo">The date to compare with.</param>
 801        /// <returns>The difference in weeks between the current date and the specified date.</returns>
 802        public int CompareWeek(DateTime dateTo)
 803        {
 24804            var ts = dateTo.Subtract(dateTime);
 24805            return ts.Days / 7;
 806        }
 807
 808        /// <summary>
 809        /// Compare two dates and return difference of week.
 810        /// </summary>
 811        /// <param name="dt2">The date to compare with.</param>
 812        /// <returns>The difference in weeks between the current date and the specified date.</returns>
 12813        public int NumberOfWeeks(DateTime dt2) => Math.Abs(dateTime.CompareWeek(dt2));
 814
 815        /// <summary>
 816        /// Compare two dates and return difference of month (positive or negative).
 817        /// </summary>
 24818        public int CompareMonth(DateTime dt2) => ((dt2.Year - dateTime.Year) * 12) + (dt2.Month - dateTime.Month);
 819
 820        /// <summary>
 821        /// Compare two dates and return difference of month.
 822        /// </summary>
 12823        public int NumberOfMonths(DateTime dt2) => Math.Abs(dateTime.CompareMonth(dt2));
 824
 825        /// <summary>
 826        /// Compare two dates and return difference of month (positive or negative).
 827        /// </summary>
 24828        public int CompareYear(DateTime dt2) => dt2.Year - dateTime.Year;
 829
 830        /// <summary>
 831        /// Compare two dates and return difference of month.
 832        /// </summary>
 12833        public int NumberOfYears(DateTime dt2) => Math.Abs(dateTime.CompareYear(dt2));
 834
 835        /// <summary>
 836        /// Returns the date with day-of-month discarded (keeps month and year) ? useful for monthly calculations.
 837        /// </summary>
 838        /// <returns>A <see cref="DateTime"/> set to the first day of the month at midnight.</returns>
 0839        public DateTime DiscardDayTime() => new(dateTime.Year, dateTime.Month, 1, 0, 0, 0, dateTime.Kind);
 840
 841        /// <summary>
 842        /// Returns the date with the time portion discarded (time set to midnight).
 843        /// </summary>
 844        /// <returns>A <see cref="DateTime"/> with time portion set to 00:00:00.</returns>
 21845        public DateTime DiscardTime() => dateTime.Date;
 846
 847        /// <summary>
 848        /// Determines whether the date lies within the inclusive range defined by start and end.
 849        /// </summary>
 850        /// <param name="start">Range start.</param>
 851        /// <param name="end">Range end.</param>
 852        /// <param name="discardTime">If <c>true</c> compare only date parts for equality to boundaries.</param>
 853        /// <returns><c>true</c> when the date is within the range; otherwise <c>false</c>.</returns>
 21854        public bool InRange(DateTime start, DateTime end, bool discardTime = true) => (discardTime && (dateTime.SameDay(
 855
 856        /// <summary>
 857        /// Orders and normalizes date-only bounds (discards time, ascending order).
 858        /// </summary>
 859        /// <param name="end">The other end of the range.</param>
 860        /// <returns>Normalized start and end dates.</returns>
 861        public (DateTime Start, DateTime End) Normalize(DateTime end) =>
 6862            dateTime.IsBefore(end)
 6863                ? (dateTime.DiscardTime(), end.DiscardTime())
 6864                : (end.DiscardTime(), dateTime.DiscardTime());
 865
 866        /// <summary>
 867        /// Calculates the age in years from a birthdate to now (UTC based).
 868        /// </summary>
 869        /// <returns>The number of full years elapsed since <paramref name="dateTime"/>.</returns>
 870        public int Age()
 871        {
 0872            var today = DateTime.UtcNow;
 873
 0874            var age = today.Year - dateTime.Year;
 875
 0876            if (today.Month < dateTime.Month || (today.Month == dateTime.Month && today.Day < dateTime.Day))
 0877                age--;
 878
 0879            return age;
 880        }
 881
 882        /// <summary>
 883        /// Generates a sequence of DateTime values starting from a minimum value and ending at a maximum value, with a 
 884        /// </summary>
 885        /// <param name="max">The maximum value of the range.</param>
 886        /// <param name="step">The step value for each iteration.</param>
 887        /// <param name="unit">The time unit for the step.</param>
 888        /// <returns>A sequence of DateTime values within the specified range.</returns>
 889        public IEnumerable<DateTime> Range(DateTime max, int step = 1, TimeUnit unit = TimeUnit.Day)
 890        {
 0891            ArgumentOutOfRangeException.ThrowIfZero(step);
 892
 0893            Func<DateTime, DateTime> increment = unit switch
 0894            {
 0895                TimeUnit.Millisecond => x => x.AddMilliseconds(step),
 0896                TimeUnit.Second => x => x.AddSeconds(step),
 0897                TimeUnit.Minute => x => x.AddMinutes(step),
 0898                TimeUnit.Hour => x => x.AddHours(step),
 0899                TimeUnit.Day => x => x.AddDays(step),
 0900                TimeUnit.Week => x => x.AddDays(step * 7),
 0901                TimeUnit.Month => x => x.AddMonths(step),
 0902                TimeUnit.Year => x => x.AddYears(step),
 0903                _ => null!
 0904            };
 905
 0906            if (step > 0)
 907            {
 0908                for (var i = dateTime; i <= max; i = increment(i))
 0909                    yield return i;
 910            }
 911            else
 912            {
 0913                for (var i = dateTime; i >= max; i = increment(i))
 0914                    yield return i;
 915            }
 0916        }
 917    }
 918
 919    extension(DateTime? dateTime)
 920    {
 921        /// <summary>
 922        /// Returns the date or <see cref="DateTime.MinValue"/> when null.
 923        /// </summary>
 3924        public DateTime OrMinValue() => dateTime ?? DateTime.MinValue;
 925
 926        /// <summary>
 927        /// Returns the date or <see cref="DateTime.MaxValue"/> when null.
 928        /// </summary>
 0929        public DateTime OrMaxValue() => dateTime ?? DateTime.MaxValue;
 930    }
 931
 932    extension(IReadOnlyList<DateTime> dates)
 933    {
 934        /// <summary>
 935        /// Returns the earliest date in the list, or null when empty.
 936        /// </summary>
 937        public DateTime? MinOrNull()
 938        {
 3939            if (dates.Count == 0)
 0940                return null;
 941
 3942            var min = dates[0];
 18943            for (var i = 1; i < dates.Count; i++)
 944            {
 6945                if (dates[i].IsBefore(min))
 3946                    min = dates[i];
 947            }
 948
 3949            return min;
 950        }
 951
 952        /// <summary>
 953        /// Returns the latest date in the list, or null when empty.
 954        /// </summary>
 955        public DateTime? MaxOrNull()
 956        {
 3957            if (dates.Count == 0)
 0958                return null;
 959
 3960            var max = dates[0];
 18961            for (var i = 1; i < dates.Count; i++)
 962            {
 6963                if (dates[i].IsAfter(max))
 3964                    max = dates[i];
 965            }
 966
 3967            return max;
 968        }
 969    }
 970}
 971

Methods/Properties

IsConsecutiveDays(System.Collections.Generic.IEnumerable`1<System.DateTime>)
ToTimeZone(System.DateTime,System.TimeZoneInfo)
ToLocalTime(System.DateTime,System.TimeSpan)
ToUniversalTime(System.DateTime,System.TimeSpan)
ToDate(System.DateTime)
ToTime(System.DateTime)
ToDateTimePeriod(System.DateTime,MyNet.Temporal.CalendarAmount)
ToInclusiveDateRange(System.DateTime,System.DateTime)
ToDateTimePeriod(System.DateTime,System.DateTime)
IsBefore(System.DateTime,System.DateTime)
IsAfter(System.DateTime,System.DateTime)
IsBefore(System.DateTime,System.DateOnly)
IsAfter(System.DateTime,System.DateOnly)
Between(System.DateTime,System.DateTime)
Between(System.DateTime,System.DateOnly)
Add(System.DateTime,System.Int32,MyNet.Primitives.TimeUnit)
AddCalendarAmount(System.DateTime,MyNet.Temporal.CalendarAmount)
SubtractCalendarAmount(System.DateTime,MyNet.Temporal.CalendarAmount)
EndOfHour(System.DateTime)
EndOfDay(System.DateTime)
EndOfDay(System.DateTime,System.Int32)
EndOfWeek(System.DateTime,System.Nullable`1<System.DayOfWeek>)
EndOfWeek(System.DateTime,System.Int32,System.Nullable`1<System.DayOfWeek>)
EndOfMonth(System.DateTime)
EndOfMonth(System.DateTime,System.Int32)
EndOfQuarter(System.DateTime)
EndOfQuarter(System.DateTime,System.Int32)
EndOfYear(System.DateTime)
EndOfYear(System.DateTime,System.Int32)
EndOfDecade(System.DateTime)
BeginningOfHour(System.DateTime)
BeginningOfDay(System.DateTime)
BeginningOfDay(System.DateTime,System.Int32)
BeginningOfWeek(System.DateTime,System.Nullable`1<System.DayOfWeek>)
BeginningOfWeek(System.DateTime,System.Int32,System.Nullable`1<System.DayOfWeek>)
BeginningOfMonth(System.DateTime)
BeginningOfMonth(System.DateTime,System.Int32)
BeginningOfQuarter(System.DateTime)
BeginningOfQuarter(System.DateTime,System.Int32)
BeginningOfYear(System.DateTime)
BeginningOfYear(System.DateTime,System.Int32)
BeginningOfDecade(System.DateTime)
NextYear(System.DateTime)
PreviousYear(System.DateTime)
NextDay(System.DateTime)
PreviousDay(System.DateTime)
Next(System.DateTime,System.DayOfWeek)
Next(System.DateTime,System.Int32,System.Int32)
Previous(System.DateTime,System.DayOfWeek)
Previous(System.DateTime,System.Int32,System.Int32)
WeekAfter(System.DateTime)
WeekEarlier(System.DateTime)
IncreaseTime(System.DateTime,System.TimeSpan)
DecreaseTime(System.DateTime,System.TimeSpan)
At(System.DateTime,System.TimeOnly)
At(System.DateTime,System.TimeSpan)
At(System.DateTime,System.Int32,System.Int32)
At(System.DateTime,System.Int32,System.Int32,System.Int32)
At(System.DateTime,System.Int32,System.Int32,System.Int32,System.Int32)
SetHour(System.DateTime,System.Int32)
SetMinute(System.DateTime,System.Int32)
SetSecond(System.DateTime,System.Int32)
SetMillisecond(System.DateTime,System.Int32)
Midnight(System.DateTime)
Noon(System.DateTime)
SetDate(System.DateTime,System.Int32)
SetDate(System.DateTime,System.Int32,System.Int32)
SetDate(System.DateTime,System.Int32,System.Int32,System.Int32)
SetYear(System.DateTime,System.Int32)
SetMonth(System.DateTime,System.Int32)
SetDay(System.DateTime,System.Int32)
FirstDayOfQuarter(System.DateTime)
FirstDayOfMonth(System.DateTime)
LastDayOfQuarter(System.DateTime)
LastDayOfMonth(System.DateTime)
AddBusinessDays(System.DateTime,System.Int32)
SubtractBusinessDays(System.DateTime,System.Int32)
IsInFuture(System.DateTime)
IsInPast(System.DateTime)
Round(System.DateTime,MyNet.Temporal.RoundTo)
FirstDayOfWeek(System.DateTime,System.Nullable`1<System.DayOfWeek>)
FirstDayOfYear(System.DateTime)
LastDayOfWeek(System.DateTime,System.Nullable`1<System.DayOfWeek>)
LastDayOfYear(System.DateTime)
IsLastDayOfWeek(System.DateTime,System.Nullable`1<System.DayOfWeek>)
IsFirstDayOfWeek(System.DateTime,System.Nullable`1<System.DayOfWeek>)
IsWeekend(System.DateTime)
PreviousMonth(System.DateTime)
NextMonth(System.DateTime)
IsToday(System.DateTime)
SameMilliSecond(System.DateTime,System.DateTime)
SameSecond(System.DateTime,System.DateTime)
SameMinute(System.DateTime,System.DateTime)
SameHour(System.DateTime,System.DateTime)
SameDay(System.DateTime,System.DateTime)
SameWeek(System.DateTime,System.DateTime)
SameMonth(System.DateTime,System.DateTime)
SameYear(System.DateTime,System.DateTime)
SameDecade(System.DateTime,System.DateTime)
NumberOfDays(System.DateTime,System.DateTime)
CompareWeek(System.DateTime,System.DateTime)
NumberOfWeeks(System.DateTime,System.DateTime)
CompareMonth(System.DateTime,System.DateTime)
NumberOfMonths(System.DateTime,System.DateTime)
CompareYear(System.DateTime,System.DateTime)
NumberOfYears(System.DateTime,System.DateTime)
DiscardDayTime(System.DateTime)
DiscardTime(System.DateTime)
InRange(System.DateTime,System.DateTime,System.DateTime,System.Boolean)
Normalize(System.DateTime,System.DateTime)
Age(System.DateTime)
Range()
OrMinValue(System.Nullable`1<System.DateTime>)
OrMaxValue(System.Nullable`1<System.DateTime>)
MinOrNull(System.Collections.Generic.IReadOnlyList`1<System.DateTime>)
MaxOrNull(System.Collections.Generic.IReadOnlyList`1<System.DateTime>)