| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DateTimeExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Globalization; |
| | | 10 | | using System.Linq; |
| | | 11 | | using MyNet.Primitives.Intervals; |
| | | 12 | | using MyNet.Primitives.Temporal; |
| | | 13 | | |
| | | 14 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 15 | | namespace MyNet.Primitives; |
| | | 16 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 17 | | |
| | | 18 | | public 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 | | { |
| | 0 | 27 | | var dateList = dates.ToList(); |
| | 0 | 28 | | 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> |
| | 12 | 38 | | 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> |
| | 0 | 45 | | 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> |
| | 0 | 52 | | 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> |
| | 0 | 58 | | 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> |
| | 0 | 64 | | public TimeOnly ToTime() => TimeOnly.FromDateTime(dateTime); |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Creates a <see cref="Period"/> starting at this date/time and lasting the specified <see cref="FluentTimeSpa |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="timeSpan">The duration to apply.</param> |
| | | 70 | | /// <returns>A <see cref="Period"/> representing the interval.</returns> |
| | 0 | 71 | | public Period ToPeriod(FluentTimeSpan timeSpan) => new(dateTime, dateTime.AddFluentTimeSpan(timeSpan)); |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Creates an inclusive calendar-day <see cref="Period"/> (end-of-day for single-day ranges). |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="end">The other end of the range.</param> |
| | | 77 | | /// <returns>A <see cref="Period"/> suitable for calendar date selection.</returns> |
| | | 78 | | public Period ToInclusivePeriod(DateTime end) |
| | | 79 | | { |
| | 3 | 80 | | var (normalizedStart, normalizedEnd) = dateTime.Normalize(end); |
| | | 81 | | |
| | 3 | 82 | | return normalizedStart == normalizedEnd |
| | 3 | 83 | | ? normalizedStart.ToPeriod(normalizedStart.AddDays(1).AddTicks(-1)) |
| | 3 | 84 | | : normalizedStart.ToPeriod(normalizedEnd); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Creates a <see cref="Period"/> that spans between this date/time and another date/time. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="otherDateTime">The other end of the period.</param> |
| | | 91 | | /// <returns>A <see cref="Period"/> representing the interval between the two dates.</returns> |
| | 9 | 92 | | public Period ToPeriod(DateTime otherDateTime) => new(dateTime.Min(otherDateTime), dateTime.Max(otherDateTime)); |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Determines whether the current date is strictly before the specified <see cref="DateTime"/> (converted to a |
| | | 96 | | /// </summary> |
| | 18 | 97 | | public bool IsBefore(DateTime toCompareWith) => dateTime < toCompareWith; |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Determines whether the current date is strictly after the specified <see cref="DateTime"/> (converted to a < |
| | | 101 | | /// </summary> |
| | 12 | 102 | | public bool IsAfter(DateTime toCompareWith) => dateTime > toCompareWith; |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Determines whether the current date is strictly before the specified <see cref="DateOnly"/>. |
| | | 106 | | /// </summary> |
| | 0 | 107 | | public bool IsBefore(DateOnly toCompareWith) => dateTime.IsBefore(toCompareWith.ToDateTime(TimeOnly.MinValue)); |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Determines whether the current date is strictly after the specified <see cref="DateOnly"/>. |
| | | 111 | | /// </summary> |
| | 0 | 112 | | public bool IsAfter(DateOnly toCompareWith) => dateTime.IsAfter(toCompareWith.ToDateTime(TimeOnly.MinValue)); |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Calculates the absolute difference between this date/time and another date/time, returning the result as a < |
| | | 116 | | /// </summary> |
| | | 117 | | /// <param name="otherDateTime">The other date/time to compare with.</param> |
| | | 118 | | /// <returns>A <see cref="TimeSpan"/> representing the absolute difference between the two dates.</returns> |
| | 0 | 119 | | public TimeSpan Between(DateTime otherDateTime) => dateTime > otherDateTime ? dateTime - otherDateTime : otherDa |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Calculates the absolute difference between this date/time and another date/time, returning the result as a < |
| | | 123 | | /// </summary> |
| | | 124 | | /// <param name="toCompareWith">The other date/time to compare with.</param> |
| | | 125 | | /// <returns>A <see cref="TimeSpan"/> representing the absolute difference between the two dates.</returns> |
| | 0 | 126 | | public TimeSpan Between(DateOnly toCompareWith) => dateTime > toCompareWith.ToDateTime(TimeOnly.MinValue) ? date |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Adds a value expressed in the specified <see cref="TimeUnit"/> to the date. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <param name="value">The amount to add.</param> |
| | | 132 | | /// <param name="timeUnitToGet">The unit of time for the amount.</param> |
| | | 133 | | /// <returns>The adjusted <see cref="DateTime"/>.</returns> |
| | 6 | 134 | | public DateTime Add(int value, TimeUnit timeUnitToGet) => dateTime.AddFluentTimeSpan(value.ToTimeSpan(timeUnitTo |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Returns a new <see cref="DateTime"/> that adds the value of the specified <see cref="FluentTimeSpan"/> to th |
| | | 138 | | /// </summary> |
| | | 139 | | public DateTime AddFluentTimeSpan(FluentTimeSpan timeSpan) |
| | 9 | 140 | | => dateTime.AddMonths(timeSpan.Months) |
| | 9 | 141 | | .AddYears(timeSpan.Years) |
| | 9 | 142 | | .Add(timeSpan.TimeSpan); |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Returns a new <see cref="DateTime"/> that subtracts the value of the specified <see cref="FluentTimeSpan"/> |
| | | 146 | | /// </summary> |
| | | 147 | | public DateTime SubtractFluentTimeSpan(FluentTimeSpan timeSpan) |
| | 3 | 148 | | => dateTime.AddMonths(-timeSpan.Months) |
| | 3 | 149 | | .AddYears(-timeSpan.Years) |
| | 3 | 150 | | .Subtract(timeSpan.TimeSpan); |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Returns the very end of the given hour (the last millisecond of the hour for the given <see cref="DateTime"/ |
| | | 154 | | /// </summary> |
| | 0 | 155 | | public DateTime EndOfHour() => new(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, 59, 59, 999, date |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Returns the very end of the given day (the last millisecond of the last hour for the given <see cref="DateTi |
| | | 159 | | /// </summary> |
| | 57 | 160 | | public DateTime EndOfDay() => new(dateTime.Year, dateTime.Month, dateTime.Day, 23, 59, 59, 999, dateTime.Kind); |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Returns the timezone-adjusted very end of the given day (the last millisecond of the last hour for the given |
| | | 164 | | /// </summary> |
| | | 165 | | public DateTime EndOfDay(int timeZoneOffset) |
| | 72 | 166 | | => new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 23, 59, 59, 999, dateTime.Kind) |
| | 72 | 167 | | .AddHours(timeZoneOffset); |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Returns the last day of the week changing the time to the very end of the day. Eg, 2011-12-24T06:40:20.005 = |
| | | 171 | | /// </summary> |
| | 18 | 172 | | public DateTime EndOfWeek(DayOfWeek? firstDayOfWeek = null) => dateTime.LastDayOfWeek(firstDayOfWeek).EndOfDay() |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Returns the last day of the week changing the time to the very end of the day with timezone-adjusted. Eg, 20 |
| | | 176 | | /// </summary> |
| | 0 | 177 | | public DateTime EndOfWeek(int timeZoneOffset, DayOfWeek? firstDayOfWeek = null) => dateTime.LastDayOfWeek(firstD |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Returns the last day of the month changing the time to the very end of the day. Eg, 2011-12-24T06:40:20.005 |
| | | 181 | | /// </summary> |
| | 9 | 182 | | public DateTime EndOfMonth() => dateTime.LastDayOfMonth().EndOfDay(); |
| | | 183 | | |
| | | 184 | | /// <summary> |
| | | 185 | | /// Returns the last day of the month changing the time to the very end of the day with timezone-adjusted. Eg, 2 |
| | | 186 | | /// </summary> |
| | 0 | 187 | | public DateTime EndOfMonth(int timeZoneOffset) => dateTime.LastDayOfMonth().EndOfDay(timeZoneOffset); |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// Returns the last day of the quarter changing the time to the very end of the day. Eg, 2011-12-24T06:40:20.00 |
| | | 191 | | /// </summary> |
| | 9 | 192 | | public DateTime EndOfQuarter() => dateTime.LastDayOfQuarter().EndOfDay(); |
| | | 193 | | |
| | | 194 | | /// <summary> |
| | | 195 | | /// Returns the last day of the quarter changing the time to the very end of the day with timezone-adjusted. Eg, |
| | | 196 | | /// </summary> |
| | 0 | 197 | | public DateTime EndOfQuarter(int timeZoneOffset) => dateTime.LastDayOfQuarter().EndOfDay(timeZoneOffset); |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Returns the last day of the year changing the time to the very end of the day. Eg, 2011-12-24T06:40:20.005 = |
| | | 201 | | /// </summary> |
| | 18 | 202 | | public DateTime EndOfYear() => dateTime.LastDayOfYear().EndOfDay(); |
| | | 203 | | |
| | | 204 | | /// <summary> |
| | | 205 | | /// Returns the last day of the year changing the time to the very end of the day with timezone-adjusted. Eg, 20 |
| | | 206 | | /// </summary> |
| | 0 | 207 | | public DateTime EndOfYear(int timeZoneOffset) => dateTime.LastDayOfYear().EndOfDay(timeZoneOffset); |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Returns the last day of the year changing the time to the very end of the day. Eg, 2011-12-24T06:40:20.005 = |
| | | 211 | | /// </summary> |
| | 9 | 212 | | public DateTime EndOfDecade() => dateTime.SetYear(dateTime.Year - (dateTime.Year % 10) + 9).EndOfYear(); |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Returns the Start of the given day (the first millisecond of the given <see cref="DateTime"/>). |
| | | 216 | | /// </summary> |
| | 0 | 217 | | public DateTime BeginningOfHour() => new(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, 0, 0, 0, da |
| | | 218 | | |
| | | 219 | | /// <summary> |
| | | 220 | | /// Returns the Start of the given day (the first millisecond of the given <see cref="DateTime"/>). |
| | | 221 | | /// </summary> |
| | 108 | 222 | | public DateTime BeginningOfDay() => new(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, 0, dateTime.Kind); |
| | | 223 | | |
| | | 224 | | /// <summary> |
| | | 225 | | /// Returns the timezone-adjusted Start of the given day (the first millisecond of the given <see cref="DateTime |
| | | 226 | | /// </summary> |
| | | 227 | | public DateTime BeginningOfDay(int timezoneOffset) |
| | 72 | 228 | | => new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, 0, dateTime.Kind) |
| | 72 | 229 | | .AddHours(timezoneOffset); |
| | | 230 | | |
| | | 231 | | /// <summary> |
| | | 232 | | /// Returns the Start day of the week changing the time to the very start of the day. Eg, 2011-12-24T06:40:20.00 |
| | | 233 | | /// </summary> |
| | 18 | 234 | | public DateTime BeginningOfWeek(DayOfWeek? firstDayOfWeek = null) => dateTime.FirstDayOfWeek(firstDayOfWeek).Beg |
| | | 235 | | |
| | | 236 | | /// <summary> |
| | | 237 | | /// Returns the Start day of the week changing the time to the very start of the day with timezone-adjusted. Eg, |
| | | 238 | | /// </summary> |
| | 0 | 239 | | public DateTime BeginningOfWeek(int timezoneOffset, DayOfWeek? firstDayOfWeek = null) => dateTime.FirstDayOfWeek |
| | | 240 | | |
| | | 241 | | /// <summary> |
| | | 242 | | /// Returns the Start day of the month changing the time to the very start of the day. Eg, 2011-12-24T06:40:20.0 |
| | | 243 | | /// </summary> |
| | 9 | 244 | | public DateTime BeginningOfMonth() => dateTime.FirstDayOfMonth().BeginningOfDay(); |
| | | 245 | | |
| | | 246 | | /// <summary> |
| | | 247 | | /// Returns the Start day of the month changing the time to the very start of the day with timezone-adjusted. Eg |
| | | 248 | | /// </summary> |
| | 0 | 249 | | public DateTime BeginningOfMonth(int timezoneOffset) => dateTime.FirstDayOfMonth().BeginningOfDay(timezoneOffset |
| | | 250 | | |
| | | 251 | | /// <summary> |
| | | 252 | | /// Returns the Start day of the quarter changing the time to the very start of the day. Eg, 2011-12-24T06:40:20 |
| | | 253 | | /// </summary> |
| | 9 | 254 | | public DateTime BeginningOfQuarter() => dateTime.FirstDayOfQuarter().BeginningOfDay(); |
| | | 255 | | |
| | | 256 | | /// <summary> |
| | | 257 | | /// Returns the Start day of the quarter changing the time to the very start of the day with timezone-adjusted. |
| | | 258 | | /// </summary> |
| | 0 | 259 | | public DateTime BeginningOfQuarter(int timezoneOffset) => dateTime.FirstDayOfQuarter().BeginningOfDay(timezoneOf |
| | | 260 | | |
| | | 261 | | /// <summary> |
| | | 262 | | /// Returns the Start day of the year changing the time to the very start of the day. Eg, 2011-12-24T06:40:20.00 |
| | | 263 | | /// </summary> |
| | 18 | 264 | | public DateTime BeginningOfYear() => dateTime.FirstDayOfYear().BeginningOfDay(); |
| | | 265 | | |
| | | 266 | | /// <summary> |
| | | 267 | | /// Returns the Start day of the year changing the time to the very start of the day with timezone-adjusted. Eg, |
| | | 268 | | /// </summary> |
| | 0 | 269 | | public DateTime BeginningOfYear(int timezoneOffset) => dateTime.FirstDayOfYear().BeginningOfDay(timezoneOffset); |
| | | 270 | | |
| | | 271 | | /// <summary> |
| | | 272 | | /// Returns the Start day of the year changing the time to the very start of the day with timezone-adjusted. Eg, |
| | | 273 | | /// </summary> |
| | 9 | 274 | | public DateTime BeginningOfDecade() => dateTime.SetYear(dateTime.Year - (dateTime.Year % 10)).BeginningOfYear(); |
| | | 275 | | |
| | | 276 | | /// <summary> |
| | | 277 | | /// Returns the same date (same Day, Month, Hour, Minute, Second etc.) in the next calendar year. |
| | | 278 | | /// If that day does not exist in next year in same month, number of missing days is added to the last day in sa |
| | | 279 | | /// </summary> |
| | | 280 | | public DateTime NextYear() |
| | | 281 | | { |
| | 6 | 282 | | var nextYear = dateTime.Year + 1; |
| | 6 | 283 | | var numberOfDaysInSameMonthNextYear = DateTime.DaysInMonth(nextYear, dateTime.Month); |
| | | 284 | | |
| | 6 | 285 | | if (numberOfDaysInSameMonthNextYear >= dateTime.Day) |
| | 3 | 286 | | return new(nextYear, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, date |
| | | 287 | | |
| | 3 | 288 | | var differenceInDays = dateTime.Day - numberOfDaysInSameMonthNextYear; |
| | 3 | 289 | | var dateTime1 = new DateTime(nextYear, dateTime.Month, numberOfDaysInSameMonthNextYear, dateTime.Hour, dateT |
| | 3 | 290 | | return dateTime1 + differenceInDays.Days(); |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | /// <summary> |
| | | 294 | | /// Returns the same date (same Day, Month, Hour, Minute, Second etc.) in the previous calendar year. |
| | | 295 | | /// If that day does not exist in previous year in same month, number of missing days is added to the last day i |
| | | 296 | | /// </summary> |
| | | 297 | | public DateTime PreviousYear() |
| | | 298 | | { |
| | 6 | 299 | | var previousYear = dateTime.Year - 1; |
| | 6 | 300 | | var numberOfDaysInSameMonthPreviousYear = DateTime.DaysInMonth(previousYear, dateTime.Month); |
| | | 301 | | |
| | 6 | 302 | | if (numberOfDaysInSameMonthPreviousYear >= dateTime.Day) |
| | 3 | 303 | | return new(previousYear, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, |
| | | 304 | | |
| | 3 | 305 | | var differenceInDays = dateTime.Day - numberOfDaysInSameMonthPreviousYear; |
| | 3 | 306 | | var dateTime1 = new DateTime(previousYear, dateTime.Month, numberOfDaysInSameMonthPreviousYear, dateTime.Hou |
| | 3 | 307 | | return dateTime1 + differenceInDays.Days(); |
| | | 308 | | } |
| | | 309 | | |
| | | 310 | | /// <summary> |
| | | 311 | | /// Returns <see cref="DateTime"/> increased by 24 hours ie Next Day. |
| | | 312 | | /// </summary> |
| | 45 | 313 | | public DateTime NextDay() => dateTime + 1.Days(); |
| | | 314 | | |
| | | 315 | | /// <summary> |
| | | 316 | | /// Returns <see cref="DateTime"/> decreased by 24h period ie Previous Day. |
| | | 317 | | /// </summary> |
| | 45 | 318 | | public DateTime PreviousDay() => dateTime - 1.Days(); |
| | | 319 | | |
| | | 320 | | /// <summary> |
| | | 321 | | /// Returns first next occurrence of specified <see cref="DayOfWeek"/>. |
| | | 322 | | /// </summary> |
| | | 323 | | public DateTime Next(DayOfWeek day) |
| | | 324 | | { |
| | | 325 | | do |
| | | 326 | | { |
| | 42 | 327 | | dateTime = dateTime.NextDay(); |
| | | 328 | | } |
| | 42 | 329 | | while (dateTime.DayOfWeek != day); |
| | | 330 | | |
| | 6 | 331 | | return dateTime; |
| | | 332 | | } |
| | | 333 | | |
| | | 334 | | /// <summary> |
| | | 335 | | /// Returns first next occurrence of specified <see cref="DayOfWeek"/>. |
| | | 336 | | /// </summary> |
| | | 337 | | public DateTime Next(int day, int month) |
| | | 338 | | { |
| | | 339 | | do |
| | | 340 | | { |
| | 0 | 341 | | dateTime = dateTime.NextDay(); |
| | | 342 | | } |
| | 0 | 343 | | while (dateTime.Month != month || dateTime.Day != day); |
| | | 344 | | |
| | 0 | 345 | | return dateTime; |
| | | 346 | | } |
| | | 347 | | |
| | | 348 | | /// <summary> |
| | | 349 | | /// Returns first next occurrence of specified <see cref="DayOfWeek"/>. |
| | | 350 | | /// </summary> |
| | | 351 | | public DateTime Previous(DayOfWeek day) |
| | | 352 | | { |
| | | 353 | | do |
| | | 354 | | { |
| | 42 | 355 | | dateTime = dateTime.PreviousDay(); |
| | | 356 | | } |
| | 42 | 357 | | while (dateTime.DayOfWeek != day); |
| | | 358 | | |
| | 6 | 359 | | return dateTime; |
| | | 360 | | } |
| | | 361 | | |
| | | 362 | | /// <summary> |
| | | 363 | | /// Returns first next occurrence of specified <see cref="DayOfWeek"/>. |
| | | 364 | | /// </summary> |
| | | 365 | | public DateTime Previous(int day, int month) |
| | | 366 | | { |
| | | 367 | | do |
| | | 368 | | { |
| | 0 | 369 | | dateTime = dateTime.PreviousDay(); |
| | | 370 | | } |
| | 0 | 371 | | while (dateTime.Month != month || dateTime.Day != day); |
| | | 372 | | |
| | 0 | 373 | | return dateTime; |
| | | 374 | | } |
| | | 375 | | |
| | | 376 | | /// <summary> |
| | | 377 | | /// Increases supplied <see cref="DateTime"/> for 7 days ie returns the Next Week. |
| | | 378 | | /// </summary> |
| | 3 | 379 | | public DateTime WeekAfter() => dateTime + 1.Weeks(); |
| | | 380 | | |
| | | 381 | | /// <summary> |
| | | 382 | | /// Decreases supplied <see cref="DateTime"/> for 7 days ie returns the Previous Week. |
| | | 383 | | /// </summary> |
| | 3 | 384 | | public DateTime WeekEarlier() => dateTime - 1.Weeks(); |
| | | 385 | | |
| | | 386 | | /// <summary> |
| | | 387 | | /// Increases the <see cref="DateTime"/> object with given <see cref="TimeSpan"/> value. |
| | | 388 | | /// </summary> |
| | 0 | 389 | | public DateTime IncreaseTime(TimeSpan toAdd) => dateTime + toAdd; |
| | | 390 | | |
| | | 391 | | /// <summary> |
| | | 392 | | /// Decreases the <see cref="DateTime"/> object with given <see cref="TimeSpan"/> value. |
| | | 393 | | /// </summary> |
| | 0 | 394 | | public DateTime DecreaseTime(TimeSpan toSubtract) => dateTime - toSubtract; |
| | | 395 | | |
| | | 396 | | /// <summary> |
| | | 397 | | /// Returns the original <see cref="DateTime"/> with Hour part changed to supplied hour parameter. |
| | | 398 | | /// </summary> |
| | 0 | 399 | | public DateTime At(TimeOnly time) => new(dateTime.Year, dateTime.Month, dateTime.Day, time.Hour, time.Minute, ti |
| | | 400 | | |
| | | 401 | | /// <summary> |
| | | 402 | | /// Returns the original <see cref="DateTime"/> with Hour part changed to supplied hour parameter. |
| | | 403 | | /// </summary> |
| | 0 | 404 | | public DateTime At(TimeSpan time) => new(dateTime.Year, dateTime.Month, dateTime.Day, time.Hours, time.Minutes, |
| | | 405 | | |
| | | 406 | | /// <summary> |
| | | 407 | | /// Returns the original <see cref="DateTime"/> with Hour and Minute parts changed to supplied hour and minute p |
| | | 408 | | /// </summary> |
| | 18 | 409 | | public DateTime At(int hour, int minute) => new(dateTime.Year, dateTime.Month, dateTime.Day, hour, minute, 0, 0, |
| | | 410 | | |
| | | 411 | | /// <summary> |
| | | 412 | | /// Returns the original <see cref="DateTime"/> with Hour, Minute and Second parts changed to supplied hour, min |
| | | 413 | | /// </summary> |
| | 18 | 414 | | public DateTime At(int hour, int minute, int second) => new(dateTime.Year, dateTime.Month, dateTime.Day, hour, m |
| | | 415 | | |
| | | 416 | | /// <summary> |
| | | 417 | | /// Returns the original <see cref="DateTime"/> with Hour, Minute, Second and Millisecond parts changed to suppl |
| | | 418 | | /// </summary> |
| | 21 | 419 | | public DateTime At(int hour, int minute, int second, int millisecond) => new(dateTime.Year, dateTime.Month, date |
| | | 420 | | |
| | | 421 | | /// <summary> |
| | | 422 | | /// Returns <see cref="DateTime"/> with changed Hour part. |
| | | 423 | | /// </summary> |
| | 87 | 424 | | public DateTime SetHour(int hour) => new(dateTime.Year, dateTime.Month, dateTime.Day, hour, dateTime.Minute, dat |
| | | 425 | | |
| | | 426 | | /// <summary> |
| | | 427 | | /// Returns <see cref="DateTime"/> with changed Minute part. |
| | | 428 | | /// </summary> |
| | 72 | 429 | | public DateTime SetMinute(int minute) => new(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, minute, |
| | | 430 | | |
| | | 431 | | /// <summary> |
| | | 432 | | /// Returns <see cref="DateTime"/> with changed Second part. |
| | | 433 | | /// </summary> |
| | 72 | 434 | | public DateTime SetSecond(int second) => new(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTim |
| | | 435 | | |
| | | 436 | | /// <summary> |
| | | 437 | | /// Returns <see cref="DateTime"/> with changed Millisecond part. |
| | | 438 | | /// </summary> |
| | 72 | 439 | | public DateTime SetMillisecond(int millisecond) => new(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hou |
| | | 440 | | |
| | | 441 | | /// <summary> |
| | | 442 | | /// Returns original <see cref="DateTime"/> value with time part set to midnight (alias for <see cref="Beginning |
| | | 443 | | /// </summary> |
| | 3 | 444 | | public DateTime Midnight() => dateTime.BeginningOfDay(); |
| | | 445 | | |
| | | 446 | | /// <summary> |
| | | 447 | | /// Returns original <see cref="DateTime"/> value with time part set to Noon (12:00:00h). |
| | | 448 | | /// </summary> |
| | | 449 | | /// <returns>A <see cref="DateTime"/> value with time part set to Noon (12:00:00h).</returns> |
| | 3 | 450 | | public DateTime Noon() => dateTime.At(12, 0, 0, 0); |
| | | 451 | | |
| | | 452 | | /// <summary> |
| | | 453 | | /// Returns <see cref="DateTime"/> with changed Year part. |
| | | 454 | | /// </summary> |
| | 0 | 455 | | public DateTime SetDate(int year) => new(year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dat |
| | | 456 | | |
| | | 457 | | /// <summary> |
| | | 458 | | /// Returns <see cref="DateTime"/> with changed Year and Month part. |
| | | 459 | | /// </summary> |
| | 0 | 460 | | public DateTime SetDate(int year, int month) => new(year, month, dateTime.Day, dateTime.Hour, dateTime.Minute, d |
| | | 461 | | |
| | | 462 | | /// <summary> |
| | | 463 | | /// Returns <see cref="DateTime"/> with changed Year, Month and Day part. |
| | | 464 | | /// </summary> |
| | 108 | 465 | | public DateTime SetDate(int year, int month, int day) => new(year, month, day, dateTime.Hour, dateTime.Minute, d |
| | | 466 | | |
| | | 467 | | /// <summary> |
| | | 468 | | /// Returns <see cref="DateTime"/> with changed Year part. |
| | | 469 | | /// </summary> |
| | 18 | 470 | | public DateTime SetYear(int year) => new(year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dat |
| | | 471 | | |
| | | 472 | | /// <summary> |
| | | 473 | | /// Returns <see cref="DateTime"/> with changed Month part. |
| | | 474 | | /// </summary> |
| | 21 | 475 | | public DateTime SetMonth(int month) => new(dateTime.Year, month, dateTime.Day, dateTime.Hour, dateTime.Minute, d |
| | | 476 | | |
| | | 477 | | /// <summary> |
| | | 478 | | /// Returns <see cref="DateTime"/> with changed Day part. |
| | | 479 | | /// </summary> |
| | 69 | 480 | | public DateTime SetDay(int day) => new(dateTime.Year, dateTime.Month, day, dateTime.Hour, dateTime.Minute, dateT |
| | | 481 | | |
| | | 482 | | /// <summary> |
| | | 483 | | /// Sets the day of the <see cref="DateTime"/> to the first day in that calendar quarter. |
| | | 484 | | /// credit to http://www.devcurry.com/2009/05/find-first-and-last-day-of-current.html. |
| | | 485 | | /// </summary> |
| | | 486 | | /// <returns>given <see cref="DateTime"/> with the day part set to the first day in the quarter.</returns> |
| | | 487 | | public DateTime FirstDayOfQuarter() |
| | | 488 | | { |
| | 21 | 489 | | var currentQuarter = ((dateTime.Month - 1) / 3) + 1; |
| | 21 | 490 | | var firstDay = new DateTime(dateTime.Year, (3 * currentQuarter) - 2, 1, 0, 0, 0, dateTime.Kind); |
| | | 491 | | |
| | 21 | 492 | | return dateTime.SetDate(firstDay.Year, firstDay.Month, firstDay.Day); |
| | | 493 | | } |
| | | 494 | | |
| | | 495 | | /// <summary> |
| | | 496 | | /// Sets the day of the <see cref="DateTime"/> to the first day in that month. |
| | | 497 | | /// </summary> |
| | | 498 | | /// <returns>given <see cref="DateTime"/> with the day part set to the first day in that month.</returns> |
| | 12 | 499 | | public DateTime FirstDayOfMonth() => dateTime.SetDay(1); |
| | | 500 | | |
| | | 501 | | /// <summary> |
| | | 502 | | /// Sets the day of the <see cref="DateTime"/> to the last day in that calendar quarter. |
| | | 503 | | /// credit to http://www.devcurry.com/2009/05/find-first-and-last-day-of-current.html. |
| | | 504 | | /// </summary> |
| | | 505 | | /// <returns>given <see cref="DateTime"/> with the day part set to the last day in the quarter.</returns> |
| | | 506 | | public DateTime LastDayOfQuarter() |
| | | 507 | | { |
| | 21 | 508 | | var currentQuarter = ((dateTime.Month - 1) / 3) + 1; |
| | 21 | 509 | | var firstDay = dateTime.SetDate(dateTime.Year, (3 * currentQuarter) - 2, 1); |
| | 21 | 510 | | return firstDay.SetMonth(firstDay.Month + 2).LastDayOfMonth(); |
| | | 511 | | } |
| | | 512 | | |
| | | 513 | | /// <summary> |
| | | 514 | | /// Sets the day of the <see cref="DateTime"/> to the last day in that month. |
| | | 515 | | /// </summary> |
| | | 516 | | /// <returns>given <see cref="DateTime"/> with the day part set to the last day in that month.</returns> |
| | 45 | 517 | | public DateTime LastDayOfMonth() => dateTime.SetDay(DateTime.DaysInMonth(dateTime.Year, dateTime.Month)); |
| | | 518 | | |
| | | 519 | | /// <summary> |
| | | 520 | | /// Adds the given number of business days to the <see cref="DateTime"/>. |
| | | 521 | | /// </summary> |
| | | 522 | | /// <param name="days">Number of business days to be added.</param> |
| | | 523 | | /// <returns>A <see cref="DateTime"/> increased by a given number of business days.</returns> |
| | | 524 | | public DateTime AddBusinessDays(int days) |
| | | 525 | | { |
| | 12 | 526 | | var sign = Math.Sign(days); |
| | 12 | 527 | | var unsignedDays = Math.Abs(days); |
| | 72 | 528 | | for (var i = 0; i < unsignedDays; i++) |
| | | 529 | | { |
| | | 530 | | do |
| | | 531 | | { |
| | 48 | 532 | | dateTime = dateTime.AddDays(sign); |
| | | 533 | | } |
| | 48 | 534 | | while (dateTime.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday); |
| | | 535 | | } |
| | | 536 | | |
| | 12 | 537 | | return dateTime; |
| | | 538 | | } |
| | | 539 | | |
| | | 540 | | /// <summary> |
| | | 541 | | /// Subtracts the given number of business days to the <see cref="DateTime"/>. |
| | | 542 | | /// </summary> |
| | | 543 | | /// <param name="days">Number of business days to be subtracted.</param> |
| | | 544 | | /// <returns>A <see cref="DateTime"/> increased by a given number of business days.</returns> |
| | 6 | 545 | | public DateTime SubtractBusinessDays(int days) => dateTime.AddBusinessDays(-days); |
| | | 546 | | |
| | | 547 | | /// <summary> |
| | | 548 | | /// Determine if a <see cref="DateTime"/> is in the future. |
| | | 549 | | /// </summary> |
| | | 550 | | /// <returns><c>true</c> if <paramref name="dateTime"/> is in the future; otherwise <c>false</c>.</returns> |
| | 9 | 551 | | public bool IsInFuture() => dateTime.ToUniversalTime() > DateTime.UtcNow; |
| | | 552 | | |
| | | 553 | | /// <summary> |
| | | 554 | | /// Determine if a <see cref="DateTime"/> is in the past. |
| | | 555 | | /// </summary> |
| | | 556 | | /// <returns><c>true</c> if <paramref name="dateTime"/> is in the past; otherwise <c>false</c>.</returns> |
| | 6 | 557 | | public bool IsInPast() => dateTime.ToUniversalTime() < DateTime.UtcNow; |
| | | 558 | | |
| | | 559 | | /// <summary> |
| | | 560 | | /// Rounds <paramref name="dateTime"/> to the nearest <see cref="RoundTo"/>. |
| | | 561 | | /// </summary> |
| | | 562 | | /// <returns>The rounded <see cref="DateTime"/>.</returns> |
| | | 563 | | public DateTime Round(RoundTo rt) |
| | | 564 | | { |
| | | 565 | | DateTime rounded; |
| | | 566 | | |
| | | 567 | | switch (rt) |
| | | 568 | | { |
| | | 569 | | case RoundTo.Second: |
| | | 570 | | { |
| | 0 | 571 | | rounded = new(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateT |
| | 0 | 572 | | if (dateTime.Millisecond >= 500) |
| | | 573 | | { |
| | 0 | 574 | | rounded = rounded.AddSeconds(1); |
| | | 575 | | } |
| | | 576 | | |
| | 0 | 577 | | break; |
| | | 578 | | } |
| | | 579 | | |
| | | 580 | | case RoundTo.Minute: |
| | | 581 | | { |
| | 0 | 582 | | rounded = new(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, 0, da |
| | 0 | 583 | | if (dateTime.Second >= 30) |
| | | 584 | | { |
| | 0 | 585 | | rounded = rounded.AddMinutes(1); |
| | | 586 | | } |
| | | 587 | | |
| | 0 | 588 | | break; |
| | | 589 | | } |
| | | 590 | | |
| | | 591 | | case RoundTo.Hour: |
| | | 592 | | { |
| | 0 | 593 | | rounded = new(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, 0, 0, dateTime.Kind); |
| | 0 | 594 | | if (dateTime.Minute >= 30) |
| | | 595 | | { |
| | 0 | 596 | | rounded = rounded.AddHours(1); |
| | | 597 | | } |
| | | 598 | | |
| | 0 | 599 | | break; |
| | | 600 | | } |
| | | 601 | | |
| | | 602 | | case RoundTo.Day: |
| | | 603 | | { |
| | 0 | 604 | | rounded = new(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, dateTime.Kind); |
| | 0 | 605 | | if (dateTime.Hour >= 12) |
| | | 606 | | { |
| | 0 | 607 | | rounded = rounded.AddDays(1); |
| | | 608 | | } |
| | | 609 | | |
| | 0 | 610 | | break; |
| | | 611 | | } |
| | | 612 | | |
| | | 613 | | default: |
| | | 614 | | { |
| | 0 | 615 | | throw new ArgumentOutOfRangeException(nameof(rt)); |
| | | 616 | | } |
| | | 617 | | } |
| | | 618 | | |
| | 0 | 619 | | return rounded; |
| | | 620 | | } |
| | | 621 | | |
| | | 622 | | /// <summary> |
| | | 623 | | /// Returns a DateTime adjusted to the beginning of the week. |
| | | 624 | | /// </summary> |
| | | 625 | | /// <returns>A DateTime instance adjusted to the beginning of the current week.</returns> |
| | | 626 | | /// <remarks>the beginning of the week is controlled by the current Culture.</remarks> |
| | | 627 | | public DateTime FirstDayOfWeek(DayOfWeek? firstDayOfWeek = null) |
| | | 628 | | { |
| | 87 | 629 | | var currentCulture = CultureInfo.CurrentCulture; |
| | 87 | 630 | | var firstDay = firstDayOfWeek ?? currentCulture.DateTimeFormat.FirstDayOfWeek; |
| | 87 | 631 | | var offset = dateTime.DayOfWeek < firstDay ? 7 : 0; |
| | 87 | 632 | | var numberOfDaysSinceBeginningOfTheWeek = dateTime.DayOfWeek + offset - firstDay; |
| | | 633 | | |
| | 87 | 634 | | return dateTime.AddDays(-numberOfDaysSinceBeginningOfTheWeek); |
| | | 635 | | } |
| | | 636 | | |
| | | 637 | | /// <summary> |
| | | 638 | | /// Returns the first day of the year keeping the time component intact. Eg, 2011-02-04T06:40:20.005 => 2011-01- |
| | | 639 | | /// </summary> |
| | 27 | 640 | | public DateTime FirstDayOfYear() => dateTime.SetDate(dateTime.Year, 1, 1); |
| | | 641 | | |
| | | 642 | | /// <summary> |
| | | 643 | | /// Returns the last day of the week keeping the time component intact. Eg, 2011-12-24T06:40:20.005 => 2011-12-2 |
| | | 644 | | /// </summary> |
| | 27 | 645 | | public DateTime LastDayOfWeek(DayOfWeek? firstDayOfWeek = null) => dateTime.FirstDayOfWeek(firstDayOfWeek).AddDa |
| | | 646 | | |
| | | 647 | | /// <summary> |
| | | 648 | | /// Returns the last day of the year keeping the time component intact. Eg, 2011-12-24T06:40:20.005 => 2011-12-3 |
| | | 649 | | /// </summary> |
| | 27 | 650 | | public DateTime LastDayOfYear() => dateTime.SetDate(dateTime.Year, 12, 31); |
| | | 651 | | |
| | | 652 | | /// <summary> |
| | | 653 | | /// Determines whether the <see cref="DateTime"/> is the last day of the week. The beginning of the week is dete |
| | | 654 | | /// </summary> |
| | | 655 | | /// <param name="firstDayOfWeek">The first day of the week. If null, the current culture's first day of the week |
| | | 656 | | /// <returns><c>true</c> if the <see cref="DateTime"/> is the last day of the week; otherwise, <c>false</c>.</re |
| | 0 | 657 | | public bool IsLastDayOfWeek(DayOfWeek? firstDayOfWeek = null) => dateTime.DayOfWeek == dateTime.LastDayOfWeek(fi |
| | | 658 | | |
| | | 659 | | /// <summary> |
| | | 660 | | /// Determines whether the <see cref="DateTime"/> is the first day of the week. The beginning of the week is det |
| | | 661 | | /// </summary> |
| | | 662 | | /// <param name="firstDayOfWeek">The first day of the week. If null, the current culture's first day of the week |
| | | 663 | | /// <returns><c>true</c> if the <see cref="DateTime"/> is the first day of the week; otherwise, <c>false</c>.</r |
| | 0 | 664 | | public bool IsFirstDayOfWeek(DayOfWeek? firstDayOfWeek = null) => dateTime.DayOfWeek == dateTime.FirstDayOfWeek( |
| | | 665 | | |
| | | 666 | | /// <summary> |
| | | 667 | | /// Determines whether the <see cref="DateTime"/> falls on a weekend day (Saturday or Sunday). |
| | | 668 | | /// </summary> |
| | | 669 | | /// <returns><c>true</c> if the <see cref="DateTime"/> is a weekend day; otherwise, <c>false</c>.</returns> |
| | 0 | 670 | | public bool IsWeekend() => dateTime.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday; |
| | | 671 | | |
| | | 672 | | /// <summary> |
| | | 673 | | /// Returns the previous month keeping the time component intact. Eg, 2010-01-20T06:40:20.005 => 2009-12-20T06:4 |
| | | 674 | | /// If the previous month doesn't have that many days the last day of the previous month is used. Eg, 2009-03-31 |
| | | 675 | | /// </summary> |
| | | 676 | | public DateTime PreviousMonth() |
| | | 677 | | { |
| | 6 | 678 | | var year = dateTime.Month == 1 ? dateTime.Year - 1 : dateTime.Year; |
| | | 679 | | |
| | 6 | 680 | | var month = dateTime.Month == 1 ? 12 : dateTime.Month - 1; |
| | | 681 | | |
| | 6 | 682 | | var firstDayOfPreviousMonth = dateTime.SetDate(year, month, 1); |
| | | 683 | | |
| | 6 | 684 | | var lastDayOfPreviousMonth = firstDayOfPreviousMonth.LastDayOfMonth().Day; |
| | | 685 | | |
| | 6 | 686 | | var day = dateTime.Day > lastDayOfPreviousMonth ? lastDayOfPreviousMonth : dateTime.Day; |
| | | 687 | | |
| | 6 | 688 | | return firstDayOfPreviousMonth.SetDay(day); |
| | | 689 | | } |
| | | 690 | | |
| | | 691 | | /// <summary> |
| | | 692 | | /// Returns the next month keeping the time component intact. Eg, 2012-12-05T06:40:20.005 => 2013-01-05T06:40:20 |
| | | 693 | | /// 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 |
| | | 694 | | /// </summary> |
| | | 695 | | public DateTime NextMonth() |
| | | 696 | | { |
| | 6 | 697 | | var year = dateTime.Month == 12 ? dateTime.Year + 1 : dateTime.Year; |
| | | 698 | | |
| | 6 | 699 | | var month = dateTime.Month == 12 ? 1 : dateTime.Month + 1; |
| | | 700 | | |
| | 6 | 701 | | var firstDayOfNextMonth = dateTime.SetDate(year, month, 1); |
| | | 702 | | |
| | 6 | 703 | | var lastDayOfPreviousMonth = firstDayOfNextMonth.LastDayOfMonth().Day; |
| | | 704 | | |
| | 6 | 705 | | var day = dateTime.Day > lastDayOfPreviousMonth ? lastDayOfPreviousMonth : dateTime.Day; |
| | | 706 | | |
| | 6 | 707 | | return firstDayOfNextMonth.SetDay(day); |
| | | 708 | | } |
| | | 709 | | |
| | | 710 | | /// <summary> |
| | | 711 | | /// Determines whether the specified <see cref="DateTime"/> value is in the same day (day + month + year) as cur |
| | | 712 | | /// </summary> |
| | | 713 | | public bool IsToday() |
| | 0 | 714 | | => dateTime.Kind switch |
| | 0 | 715 | | { |
| | 0 | 716 | | DateTimeKind.Local => dateTime.SameDay(DateTime.Now), |
| | 0 | 717 | | _ => dateTime.SameDay(DateTime.UtcNow) |
| | 0 | 718 | | }; |
| | | 719 | | |
| | | 720 | | /// <summary> |
| | | 721 | | /// Determines whether the specified <see cref="DateTime"/> value is exactly the same millisecond (millisecond + |
| | | 722 | | /// </summary> |
| | | 723 | | /// <param name="date">The date to compare with.</param> |
| | | 724 | | /// <returns><c>true</c> if the specified date is exactly the same millisecond as the current date; otherwise, < |
| | 0 | 725 | | public bool SameMilliSecond(DateTime date) => dateTime.SameSecond(date) && dateTime.Millisecond == date.Millisec |
| | | 726 | | |
| | | 727 | | /// <summary> |
| | | 728 | | /// Determines whether the specified <see cref="DateTime"/> value is exactly the same second (second + minute + |
| | | 729 | | /// </summary> |
| | | 730 | | /// <param name="date">The date to compare with.</param> |
| | | 731 | | /// <returns><c>true</c> if the specified date is exactly the same second as the current date; otherwise, <c>fal |
| | 0 | 732 | | public bool SameSecond(DateTime date) => dateTime.SameMinute(date) && dateTime.Second == date.Second; |
| | | 733 | | |
| | | 734 | | /// <summary> |
| | | 735 | | /// Determines whether the specified <see cref="DateTime"/> value is exactly the same minute (minute + hour + da |
| | | 736 | | /// </summary> |
| | | 737 | | /// <param name="date">The date to compare with.</param> |
| | | 738 | | /// <returns><c>true</c> if the specified date is exactly the same minute as the current date; otherwise, <c>fal |
| | 0 | 739 | | public bool SameMinute(DateTime date) => dateTime.SameHour(date) && dateTime.Minute == date.Minute; |
| | | 740 | | |
| | | 741 | | /// <summary> |
| | | 742 | | /// Determines whether the specified <see cref="DateTime"/> value is exactly the same hour (hour + day + month + |
| | | 743 | | /// </summary> |
| | | 744 | | /// <param name="date">The date to compare with.</param> |
| | | 745 | | /// <returns><c>true</c> if the specified date is exactly the same hour as the current date; otherwise, <c>false |
| | 0 | 746 | | public bool SameHour(DateTime date) => dateTime.SameDay(date) && dateTime.Hour == date.Hour; |
| | | 747 | | |
| | | 748 | | /// <summary> |
| | | 749 | | /// Determines whether the specified <see cref="DateTime"/> value is exactly the same day (day + month + year) t |
| | | 750 | | /// </summary> |
| | | 751 | | /// <param name="date">Value to compare with.</param> |
| | | 752 | | /// <returns> |
| | | 753 | | /// <c>true</c> if the specified date is exactly the same year then current; otherwise, <c>false</c>. |
| | | 754 | | /// </returns> |
| | 51 | 755 | | public bool SameDay(DateTime date) => dateTime.Date == date.Date; |
| | | 756 | | |
| | | 757 | | /// <summary> |
| | | 758 | | /// Determines whether the specified <see cref="DateTime"/> value is exactly the same month (month + year) then |
| | | 759 | | /// </summary> |
| | | 760 | | /// <param name="date">Value to compare with.</param> |
| | | 761 | | /// <returns> |
| | | 762 | | /// <c>true</c> if the specified date is exactly the same month and year then current; otherwise, <c>false</ |
| | | 763 | | /// </returns> |
| | 0 | 764 | | public bool SameWeek(DateTime date) => date.IsAfter(dateTime.BeginningOfWeek()) && date < dateTime.EndOfWeek(); |
| | | 765 | | |
| | | 766 | | /// <summary> |
| | | 767 | | /// Determines whether the specified <see cref="DateTime"/> value is exactly the same month (month + year) then |
| | | 768 | | /// </summary> |
| | | 769 | | /// <param name="date">Value to compare with.</param> |
| | | 770 | | /// <returns> |
| | | 771 | | /// <c>true</c> if the specified date is exactly the same month and year then current; otherwise, <c>false</ |
| | | 772 | | /// </returns> |
| | 18 | 773 | | public bool SameMonth(DateTime date) => dateTime.Month == date.Month && dateTime.Year == date.Year; |
| | | 774 | | |
| | | 775 | | /// <summary> |
| | | 776 | | /// Determines whether the specified <see cref="DateTime"/> value is exactly the same year then current. Eg, 201 |
| | | 777 | | /// </summary> |
| | | 778 | | /// <param name="date">Value to compare with.</param> |
| | | 779 | | /// <returns> |
| | | 780 | | /// <c>true</c> if the specified date is exactly the same date then current; otherwise, <c>false</c>. |
| | | 781 | | /// </returns> |
| | 18 | 782 | | public bool SameYear(DateTime date) => dateTime.Year == date.Year; |
| | | 783 | | |
| | | 784 | | /// <summary> |
| | | 785 | | /// Determines whether the specified <see cref="DateTime"/> value is exactly the same year then current. Eg, 201 |
| | | 786 | | /// </summary> |
| | | 787 | | /// <param name="date">Value to compare with.</param> |
| | | 788 | | /// <returns> |
| | | 789 | | /// <c>true</c> if the specified date is exactly the same date then current; otherwise, <c>false</c>. |
| | | 790 | | /// </returns> |
| | 0 | 791 | | public bool SameDecade(DateTime date) => dateTime.BeginningOfDecade() == date.BeginningOfDecade(); |
| | | 792 | | |
| | | 793 | | /// <summary> |
| | | 794 | | /// Compare two dates and return difference of days (positive or negative). |
| | | 795 | | /// </summary> |
| | | 796 | | /// <param name="dateTo">The date to compare with.</param> |
| | | 797 | | /// <returns>The difference in days between the current date and the specified date.</returns> |
| | 9 | 798 | | public int NumberOfDays(DateTime dateTo) => Math.Abs((dateTime - dateTo).Days); |
| | | 799 | | |
| | | 800 | | /// <summary> |
| | | 801 | | /// Compare two dates and return difference of week (positive or negative). |
| | | 802 | | /// </summary> |
| | | 803 | | /// <param name="dateTo">The date to compare with.</param> |
| | | 804 | | /// <returns>The difference in weeks between the current date and the specified date.</returns> |
| | | 805 | | public int CompareWeek(DateTime dateTo) |
| | | 806 | | { |
| | 24 | 807 | | var ts = dateTo.Subtract(dateTime); |
| | 24 | 808 | | return ts.Days / 7; |
| | | 809 | | } |
| | | 810 | | |
| | | 811 | | /// <summary> |
| | | 812 | | /// Compare two dates and return difference of week. |
| | | 813 | | /// </summary> |
| | | 814 | | /// <param name="dt2">The date to compare with.</param> |
| | | 815 | | /// <returns>The difference in weeks between the current date and the specified date.</returns> |
| | 12 | 816 | | public int NumberOfWeeks(DateTime dt2) => Math.Abs(dateTime.CompareWeek(dt2)); |
| | | 817 | | |
| | | 818 | | /// <summary> |
| | | 819 | | /// Compare two dates and return difference of month (positive or negative). |
| | | 820 | | /// </summary> |
| | 24 | 821 | | public int CompareMonth(DateTime dt2) => ((dt2.Year - dateTime.Year) * 12) + (dt2.Month - dateTime.Month); |
| | | 822 | | |
| | | 823 | | /// <summary> |
| | | 824 | | /// Compare two dates and return difference of month. |
| | | 825 | | /// </summary> |
| | 12 | 826 | | public int NumberOfMonths(DateTime dt2) => Math.Abs(dateTime.CompareMonth(dt2)); |
| | | 827 | | |
| | | 828 | | /// <summary> |
| | | 829 | | /// Compare two dates and return difference of month (positive or negative). |
| | | 830 | | /// </summary> |
| | 24 | 831 | | public int CompareYear(DateTime dt2) => dt2.Year - dateTime.Year; |
| | | 832 | | |
| | | 833 | | /// <summary> |
| | | 834 | | /// Compare two dates and return difference of month. |
| | | 835 | | /// </summary> |
| | 12 | 836 | | public int NumberOfYears(DateTime dt2) => Math.Abs(dateTime.CompareYear(dt2)); |
| | | 837 | | |
| | | 838 | | /// <summary> |
| | | 839 | | /// Returns the date with day-of-month discarded (keeps month and year) — useful for monthly calculations. |
| | | 840 | | /// </summary> |
| | | 841 | | /// <returns>A <see cref="DateTime"/> set to the first day of the month at midnight.</returns> |
| | 0 | 842 | | public DateTime DiscardDayTime() => new(dateTime.Year, dateTime.Month, 1, 0, 0, 0, dateTime.Kind); |
| | | 843 | | |
| | | 844 | | /// <summary> |
| | | 845 | | /// Returns the date with the time portion discarded (time set to midnight). |
| | | 846 | | /// </summary> |
| | | 847 | | /// <returns>A <see cref="DateTime"/> with time portion set to 00:00:00.</returns> |
| | 24 | 848 | | public DateTime DiscardTime() => dateTime.Date; |
| | | 849 | | |
| | | 850 | | /// <summary> |
| | | 851 | | /// Determines whether the date lies within the inclusive range defined by start and end. |
| | | 852 | | /// </summary> |
| | | 853 | | /// <param name="start">Range start.</param> |
| | | 854 | | /// <param name="end">Range end.</param> |
| | | 855 | | /// <param name="discardTime">If <c>true</c> compare only date parts for equality to boundaries.</param> |
| | | 856 | | /// <returns><c>true</c> when the date is within the range; otherwise <c>false</c>.</returns> |
| | 21 | 857 | | public bool InRange(DateTime start, DateTime end, bool discardTime = true) => (discardTime && (dateTime.SameDay( |
| | | 858 | | |
| | | 859 | | /// <summary> |
| | | 860 | | /// Orders and normalizes date-only bounds (discards time, ascending order). |
| | | 861 | | /// </summary> |
| | | 862 | | /// <param name="end">The other end of the range.</param> |
| | | 863 | | /// <returns>Normalized start and end dates.</returns> |
| | | 864 | | public (DateTime Start, DateTime End) Normalize(DateTime end) => |
| | 6 | 865 | | dateTime.IsBefore(end) |
| | 6 | 866 | | ? (dateTime.DiscardTime(), end.DiscardTime()) |
| | 6 | 867 | | : (end.DiscardTime(), dateTime.DiscardTime()); |
| | | 868 | | |
| | | 869 | | /// <summary> |
| | | 870 | | /// Calculates the age in years from a birthdate to now (UTC based). |
| | | 871 | | /// </summary> |
| | | 872 | | /// <returns>The number of full years elapsed since <paramref name="dateTime"/>.</returns> |
| | | 873 | | public int Age() |
| | | 874 | | { |
| | 0 | 875 | | var today = DateTime.UtcNow; |
| | | 876 | | |
| | 0 | 877 | | var age = today.Year - dateTime.Year; |
| | | 878 | | |
| | 0 | 879 | | if (today.Month < dateTime.Month || (today.Month == dateTime.Month && today.Day < dateTime.Day)) |
| | 0 | 880 | | age--; |
| | | 881 | | |
| | 0 | 882 | | return age; |
| | | 883 | | } |
| | | 884 | | |
| | | 885 | | /// <summary> |
| | | 886 | | /// Generates a sequence of DateTime values starting from a minimum value and ending at a maximum value, with a |
| | | 887 | | /// </summary> |
| | | 888 | | /// <param name="max">The maximum value of the range.</param> |
| | | 889 | | /// <param name="step">The step value for each iteration.</param> |
| | | 890 | | /// <param name="unit">The time unit for the step.</param> |
| | | 891 | | /// <returns>A sequence of DateTime values within the specified range.</returns> |
| | | 892 | | public IEnumerable<DateTime> Range(DateTime max, int step = 1, TimeUnit unit = TimeUnit.Day) |
| | | 893 | | { |
| | 0 | 894 | | ArgumentOutOfRangeException.ThrowIfZero(step); |
| | | 895 | | |
| | 0 | 896 | | Func<DateTime, DateTime> increment = unit switch |
| | 0 | 897 | | { |
| | 0 | 898 | | TimeUnit.Millisecond => x => x.AddMilliseconds(step), |
| | 0 | 899 | | TimeUnit.Second => x => x.AddSeconds(step), |
| | 0 | 900 | | TimeUnit.Minute => x => x.AddMinutes(step), |
| | 0 | 901 | | TimeUnit.Hour => x => x.AddHours(step), |
| | 0 | 902 | | TimeUnit.Day => x => x.AddDays(step), |
| | 0 | 903 | | TimeUnit.Week => x => x.AddDays(step * 7), |
| | 0 | 904 | | TimeUnit.Month => x => x.AddMonths(step), |
| | 0 | 905 | | TimeUnit.Year => x => x.AddYears(step), |
| | 0 | 906 | | _ => null! |
| | 0 | 907 | | }; |
| | | 908 | | |
| | 0 | 909 | | if (step > 0) |
| | | 910 | | { |
| | 0 | 911 | | for (var i = dateTime; i <= max; i = increment(i)) |
| | 0 | 912 | | yield return i; |
| | | 913 | | } |
| | | 914 | | else |
| | | 915 | | { |
| | 0 | 916 | | for (var i = dateTime; i >= max; i = increment(i)) |
| | 0 | 917 | | yield return i; |
| | | 918 | | } |
| | 0 | 919 | | } |
| | | 920 | | } |
| | | 921 | | |
| | | 922 | | extension(DateTime? dateTime) |
| | | 923 | | { |
| | | 924 | | /// <summary> |
| | | 925 | | /// Returns the date or <see cref="DateTime.MinValue"/> when null. |
| | | 926 | | /// </summary> |
| | 3 | 927 | | public DateTime OrMinValue() => dateTime ?? DateTime.MinValue; |
| | | 928 | | |
| | | 929 | | /// <summary> |
| | | 930 | | /// Returns the date or <see cref="DateTime.MaxValue"/> when null. |
| | | 931 | | /// </summary> |
| | 0 | 932 | | public DateTime OrMaxValue() => dateTime ?? DateTime.MaxValue; |
| | | 933 | | } |
| | | 934 | | |
| | | 935 | | extension(IReadOnlyList<DateTime> dates) |
| | | 936 | | { |
| | | 937 | | /// <summary> |
| | | 938 | | /// Returns the earliest date in the list, or null when empty. |
| | | 939 | | /// </summary> |
| | | 940 | | public DateTime? MinOrNull() |
| | | 941 | | { |
| | 3 | 942 | | if (dates.Count == 0) |
| | 0 | 943 | | return null; |
| | | 944 | | |
| | 3 | 945 | | var min = dates[0]; |
| | 18 | 946 | | for (var i = 1; i < dates.Count; i++) |
| | | 947 | | { |
| | 6 | 948 | | if (dates[i].IsBefore(min)) |
| | 3 | 949 | | min = dates[i]; |
| | | 950 | | } |
| | | 951 | | |
| | 3 | 952 | | return min; |
| | | 953 | | } |
| | | 954 | | |
| | | 955 | | /// <summary> |
| | | 956 | | /// Returns the latest date in the list, or null when empty. |
| | | 957 | | /// </summary> |
| | | 958 | | public DateTime? MaxOrNull() |
| | | 959 | | { |
| | 3 | 960 | | if (dates.Count == 0) |
| | 0 | 961 | | return null; |
| | | 962 | | |
| | 3 | 963 | | var max = dates[0]; |
| | 18 | 964 | | for (var i = 1; i < dates.Count; i++) |
| | | 965 | | { |
| | 6 | 966 | | if (dates[i].IsAfter(max)) |
| | 3 | 967 | | max = dates[i]; |
| | | 968 | | } |
| | | 969 | | |
| | 3 | 970 | | return max; |
| | | 971 | | } |
| | | 972 | | } |
| | | 973 | | } |
| | | 974 | | |