| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DateOnlyExtensions.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 MyNet.Primitives.Temporal; |
| | | 11 | | |
| | | 12 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 13 | | namespace MyNet.Primitives; |
| | | 14 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 15 | | |
| | | 16 | | public static class DateOnlyExtensions |
| | | 17 | | { |
| | | 18 | | extension(DateOnly date) |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Returns the last day of the decade for the provided date. |
| | | 22 | | /// </summary> |
| | 3 | 23 | | public DateOnly EndOfDecade() => date.SetYear(date.Year - (date.Year % 10) + 9).EndOfYear(); |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Returns the first day of the decade for the provided date. |
| | | 27 | | /// </summary> |
| | 15 | 28 | | public DateOnly BeginningOfDecade() => date.SetYear(date.Year - (date.Year % 10)).BeginningOfYear(); |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Returns the same date (same Day, Month, Hour, Minute, Second etc.) in the next calendar year. |
| | | 32 | | /// If that day does not exist in next year in same month, number of missing days is added to the last day in sa |
| | | 33 | | /// </summary> |
| | | 34 | | public DateOnly NextYear() |
| | | 35 | | { |
| | 6 | 36 | | var nextYear = date.Year + 1; |
| | 6 | 37 | | var numberOfDaysInSameMonthNextYear = DateTime.DaysInMonth(nextYear, date.Month); |
| | | 38 | | |
| | 9 | 39 | | if (numberOfDaysInSameMonthNextYear >= date.Day) return new(nextYear, date.Month, date.Day); |
| | 3 | 40 | | var differenceInDays = date.Day - numberOfDaysInSameMonthNextYear; |
| | 3 | 41 | | var dateTime = new DateOnly(nextYear, date.Month, numberOfDaysInSameMonthNextYear); |
| | 3 | 42 | | return dateTime.AddDays(differenceInDays); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Returns the same date (same Day, Month, Hour, Minute, Second etc.) in the previous calendar year. |
| | | 47 | | /// If that day does not exist in previous year in same month, number of missing days is added to the last day i |
| | | 48 | | /// </summary> |
| | | 49 | | public DateOnly PreviousYear() |
| | | 50 | | { |
| | 3 | 51 | | var previousYear = date.Year - 1; |
| | 3 | 52 | | var numberOfDaysInSameMonthPreviousYear = DateTime.DaysInMonth(previousYear, date.Month); |
| | | 53 | | |
| | 3 | 54 | | if (numberOfDaysInSameMonthPreviousYear >= date.Day) |
| | 3 | 55 | | return new(previousYear, date.Month, date.Day); |
| | 0 | 56 | | var differenceInDays = date.Day - numberOfDaysInSameMonthPreviousYear; |
| | 0 | 57 | | var dateTime = new DateOnly(previousYear, date.Month, numberOfDaysInSameMonthPreviousYear); |
| | 0 | 58 | | return dateTime.AddDays(differenceInDays); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Returns the next calendar day. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <returns>The date one day after <paramref name="date"/>.</returns> |
| | 54 | 65 | | public DateOnly NextDay() => date.AddDays(1); |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Returns the previous calendar day. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <returns>The date one day before <paramref name="date"/>.</returns> |
| | 66 | 71 | | public DateOnly PreviousDay() => date.AddDays(-1); |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Returns first next occurrence of specified <see cref="DayOfWeek"/>. |
| | | 75 | | /// </summary> |
| | | 76 | | public DateOnly Next(DayOfWeek day) |
| | | 77 | | { |
| | | 78 | | do |
| | | 79 | | { |
| | 12 | 80 | | date = date.NextDay(); |
| | | 81 | | } |
| | 12 | 82 | | while (date.DayOfWeek != day); |
| | | 83 | | |
| | 3 | 84 | | return date; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Returns the next occurrence of a specific day and month. |
| | | 89 | | /// </summary> |
| | | 90 | | public DateOnly Next(int day, int month) |
| | | 91 | | { |
| | | 92 | | do |
| | | 93 | | { |
| | 39 | 94 | | date = date.NextDay(); |
| | | 95 | | } |
| | 39 | 96 | | while (date.Month != month || date.Day != day); |
| | | 97 | | |
| | 3 | 98 | | return date; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Returns the previous occurrence of the specified <see cref="DayOfWeek"/>. |
| | | 103 | | /// </summary> |
| | | 104 | | public DateOnly Previous(DayOfWeek day) |
| | | 105 | | { |
| | | 106 | | do |
| | | 107 | | { |
| | 9 | 108 | | date = date.PreviousDay(); |
| | | 109 | | } |
| | 9 | 110 | | while (date.DayOfWeek != day); |
| | | 111 | | |
| | 3 | 112 | | return date; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Returns the previous occurrence of the specified day/month. |
| | | 117 | | /// </summary> |
| | | 118 | | public DateOnly Previous(int day, int month) |
| | | 119 | | { |
| | | 120 | | do |
| | | 121 | | { |
| | 54 | 122 | | date = date.PreviousDay(); |
| | | 123 | | } |
| | 54 | 124 | | while (date.Month != month || date.Day != day); |
| | | 125 | | |
| | 3 | 126 | | return date; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Increases the date by one calendar week (number of days determined by <see cref="DateTimeHelper.DaysPerWeek" |
| | | 131 | | /// </summary> |
| | 3 | 132 | | public DateOnly WeekAfter() => date.AddDays(DateTimeHelper.DaysPerWeek); |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Decreases the date by one calendar week. |
| | | 136 | | /// </summary> |
| | 3 | 137 | | public DateOnly WeekEarlier() => date.AddDays(-DateTimeHelper.DaysPerWeek); |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Returns <see cref="DateOnly"/> with changed year component. |
| | | 141 | | /// </summary> |
| | 21 | 142 | | public DateOnly SetYear(int year) => new(year, date.Month, date.Day); |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Returns <see cref="DateOnly"/> with changed month component. |
| | | 146 | | /// </summary> |
| | 3 | 147 | | public DateOnly SetMonth(int month) => new(date.Year, month, date.Day); |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Returns <see cref="DateOnly"/> with changed day component. |
| | | 151 | | /// </summary> |
| | 45 | 152 | | public DateOnly SetDay(int day) => new(date.Year, date.Month, day); |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Determines whether the current date is strictly before the specified <see cref="DateTime"/> (converted to a |
| | | 156 | | /// </summary> |
| | 0 | 157 | | public bool IsBefore(DateTime toCompareWith) => date.ToDateTime(TimeOnly.MinValue).IsBefore(toCompareWith); |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Determines whether the current date is strictly after the specified <see cref="DateTime"/> (converted to a < |
| | | 161 | | /// </summary> |
| | 0 | 162 | | public bool IsAfter(DateTime toCompareWith) => date.ToDateTime(TimeOnly.MinValue).IsAfter(toCompareWith); |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Determines whether the current date is strictly before the specified <see cref="DateOnly"/>. |
| | | 166 | | /// </summary> |
| | 0 | 167 | | public bool IsBefore(DateOnly toCompareWith) => date.IsBefore(toCompareWith.ToDateTime(TimeOnly.MinValue)); |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Determines whether the current date is strictly after the specified <see cref="DateOnly"/>. |
| | | 171 | | /// </summary> |
| | 0 | 172 | | public bool IsAfter(DateOnly toCompareWith) => date.IsAfter(toCompareWith.ToDateTime(TimeOnly.MinValue)); |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Returns a <see cref="DateTime"/> with the time part set to the provided <see cref="TimeOnly"/>. |
| | | 176 | | /// </summary> |
| | | 177 | | /// <param name="time">The time to apply.</param> |
| | | 178 | | /// <returns>A <see cref="DateTime"/> representing the combined date and time.</returns> |
| | 12 | 179 | | public DateTime At(TimeOnly time) => date.ToDateTime(time); |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Returns a <see cref="DateTime"/> with the time part set to the provided <see cref="TimeOnly"/> and specified |
| | | 183 | | /// </summary> |
| | 0 | 184 | | public DateTime At(TimeOnly time, DateTimeKind kind) => date.ToDateTime(time, kind); |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Returns a <see cref="DateTime"/> representing the provided hour and minute on the same date. |
| | | 188 | | /// </summary> |
| | 3 | 189 | | public DateTime At(int hour, int minute) => date.At(new(hour, minute)); |
| | | 190 | | |
| | | 191 | | /// <summary> |
| | | 192 | | /// Returns a <see cref="DateTime"/> representing the provided hour, minute and second on the same date. |
| | | 193 | | /// </summary> |
| | 0 | 194 | | public DateTime At(int hour, int minute, int second) => date.At(new(hour, minute, second)); |
| | | 195 | | |
| | | 196 | | /// <summary> |
| | | 197 | | /// Returns a <see cref="DateTime"/> representing the provided hour, minute, second and milliseconds on the same |
| | | 198 | | /// </summary> |
| | 0 | 199 | | public DateTime At(int hour, int minute, int second, int milliseconds) => date.At(new(hour, minute, second, mill |
| | | 200 | | |
| | | 201 | | /// <summary> |
| | | 202 | | /// Gets the earliest possible <see cref="DateTime"/> for the date (midnight). |
| | | 203 | | /// </summary> |
| | 3 | 204 | | public DateTime BeginningOfDay() => date.At(TimeOnly.MinValue); |
| | | 205 | | |
| | | 206 | | /// <summary> |
| | | 207 | | /// Gets the latest possible <see cref="DateTime"/> for the date (end of day). |
| | | 208 | | /// </summary> |
| | 3 | 209 | | public DateTime EndOfDay() => date.At(TimeOnly.MaxValue); |
| | | 210 | | |
| | | 211 | | /// <summary> |
| | | 212 | | /// Sets the day of the <see cref="DateOnly"/> to the first day in that calendar quarter. |
| | | 213 | | /// credit to http://www.devcurry.com/2009/05/find-first-and-last-day-of-current.html. |
| | | 214 | | /// </summary> |
| | | 215 | | /// <returns>given <see cref="DateTime"/> with the day part set to the first day in the quarter.</returns> |
| | | 216 | | public DateOnly BeginningOfQuarter() |
| | | 217 | | { |
| | 3 | 218 | | var currentQuarter = ((date.Month - 1) / 3) + 1; |
| | 3 | 219 | | return new(date.Year, (3 * currentQuarter) - 2, 1); |
| | | 220 | | } |
| | | 221 | | |
| | | 222 | | /// <summary> |
| | | 223 | | /// Sets the day of the <see cref="DateOnly"/> to the first day in that month. |
| | | 224 | | /// </summary> |
| | | 225 | | /// <returns>given <see cref="DateOnly"/> with the day part set to the first day in that month.</returns> |
| | 3 | 226 | | public DateOnly BeginningOfMonth() => date.SetDay(1); |
| | | 227 | | |
| | | 228 | | /// <summary> |
| | | 229 | | /// Sets the day of the <see cref="DateOnly"/> to the last day in that calendar quarter. |
| | | 230 | | /// credit to http://www.devcurry.com/2009/05/find-first-and-last-day-of-current.html. |
| | | 231 | | /// </summary> |
| | | 232 | | /// <returns>given <see cref="DateOnly"/> with the day part set to the last day in the quarter.</returns> |
| | | 233 | | public DateOnly EndOfQuarter() |
| | | 234 | | { |
| | 0 | 235 | | var currentQuarter = ((date.Month - 1) / 3) + 1; |
| | 0 | 236 | | var firstDay = new DateOnly(date.Year, (3 * currentQuarter) - 2, 1); |
| | 0 | 237 | | return new(firstDay.Year, firstDay.Month + 2, 1); |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | /// <summary> |
| | | 241 | | /// Sets the day of the <see cref="DateOnly"/> to the last day in that month. |
| | | 242 | | /// </summary> |
| | | 243 | | /// <returns>given <see cref="DateOnly"/> with the day part set to the last day in that month.</returns> |
| | 21 | 244 | | public DateOnly EndOfMonth() => date.SetDay(DateTime.DaysInMonth(date.Year, date.Month)); |
| | | 245 | | |
| | | 246 | | /// <summary> |
| | | 247 | | /// Adds the given number of business days to the <see cref="DateOnly"/>. |
| | | 248 | | /// </summary> |
| | | 249 | | /// <param name="days">Number of business days to be added.</param> |
| | | 250 | | /// <returns>A <see cref="DateOnly"/> increased by a given number of business days.</returns> |
| | | 251 | | public DateOnly AddBusinessDays(int days) |
| | | 252 | | { |
| | 6 | 253 | | var sign = Math.Sign(days); |
| | 6 | 254 | | var unsignedDays = Math.Abs(days); |
| | 24 | 255 | | for (var i = 0; i < unsignedDays; i++) |
| | | 256 | | { |
| | | 257 | | do |
| | | 258 | | { |
| | 18 | 259 | | date = date.AddDays(sign); |
| | | 260 | | } |
| | 18 | 261 | | while (date.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday); |
| | | 262 | | } |
| | | 263 | | |
| | 6 | 264 | | return date; |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | /// <summary> |
| | | 268 | | /// Subtracts the given number of business days to the <see cref="DateOnly"/>. |
| | | 269 | | /// </summary> |
| | | 270 | | /// <param name="days">Number of business days to be subtracted.</param> |
| | | 271 | | /// <returns>A <see cref="DateOnly"/> increased by a given number of business days.</returns> |
| | 3 | 272 | | public DateOnly SubtractBusinessDays(int days) => date.AddBusinessDays(-days); |
| | | 273 | | |
| | | 274 | | /// <summary> |
| | | 275 | | /// Determine if a <see cref="DateOnly"/> is in the future compared to the current UTC date. |
| | | 276 | | /// </summary> |
| | | 277 | | /// <returns><c>true</c> if <paramref name="date"/> is in the future; otherwise <c>false</c>.</returns> |
| | 0 | 278 | | public bool IsInFuture() => date.At(TimeOnly.MinValue) > DateTime.UtcNow; |
| | | 279 | | |
| | | 280 | | /// <summary> |
| | | 281 | | /// Determine if a <see cref="DateOnly"/> is in the past compared to the current UTC date. |
| | | 282 | | /// </summary> |
| | | 283 | | /// <returns><c>true</c> if <paramref name="date"/> is in the past; otherwise <c>false</c>.</returns> |
| | 0 | 284 | | public bool IsInPast() => date.At(TimeOnly.MinValue) < DateTime.UtcNow; |
| | | 285 | | |
| | | 286 | | /// <summary> |
| | | 287 | | /// Returns a DateOnly adjusted to the beginning of the week. |
| | | 288 | | /// </summary> |
| | | 289 | | /// <returns>A DateOnly instance adjusted to the beginning of the current week.</returns> |
| | | 290 | | /// <remarks>the beginning of the week is controlled by the current Culture.</remarks> |
| | | 291 | | public DateOnly BeginningOfWeek(DayOfWeek? firstDayOfWeek = null) |
| | | 292 | | { |
| | 9 | 293 | | var currentCulture = CultureInfo.CurrentCulture; |
| | 9 | 294 | | var firstDay = firstDayOfWeek ?? currentCulture.DateTimeFormat.FirstDayOfWeek; |
| | 9 | 295 | | var offset = date.DayOfWeek < firstDay ? 7 : 0; |
| | 9 | 296 | | var numberOfDaysSinceBeginningOfTheWeek = date.DayOfWeek + offset - firstDay; |
| | | 297 | | |
| | 9 | 298 | | return date.AddDays(-numberOfDaysSinceBeginningOfTheWeek); |
| | | 299 | | } |
| | | 300 | | |
| | | 301 | | /// <summary> |
| | | 302 | | /// Returns the first day of the year keeping the time component intact. |
| | | 303 | | /// </summary> |
| | | 304 | | /// <returns>New date.</returns> |
| | 18 | 305 | | public DateOnly BeginningOfYear() => new(date.Year, 1, 1); |
| | | 306 | | |
| | | 307 | | /// <summary> |
| | | 308 | | /// Returns the last day of the week keeping the time component intact. |
| | | 309 | | /// </summary> |
| | | 310 | | /// <returns>New date.</returns> |
| | 3 | 311 | | public DateOnly EndOfWeek(DayOfWeek? firstDayOfWeek = null) => date.BeginningOfWeek(firstDayOfWeek).AddDays(6); |
| | | 312 | | |
| | | 313 | | /// <summary> |
| | | 314 | | /// Returns the last day of the year keeping the time component intact. |
| | | 315 | | /// </summary> |
| | | 316 | | /// <returns>New date.</returns> |
| | 6 | 317 | | public DateOnly EndOfYear() => new(date.Year, 12, 31); |
| | | 318 | | |
| | | 319 | | /// <summary> |
| | | 320 | | /// Determines whether the current date is the last day of the week. |
| | | 321 | | /// </summary> |
| | 0 | 322 | | public bool IsLastDayOfWeek(DayOfWeek? firstDayOfWeek = null) => date.DayOfWeek == date.EndOfWeek(firstDayOfWeek |
| | | 323 | | |
| | | 324 | | /// <summary> |
| | | 325 | | /// Determines whether the current date is the first day of the week. |
| | | 326 | | /// </summary> |
| | 0 | 327 | | public bool IsFirstDayOfWeek(DayOfWeek? firstDayOfWeek = null) => date.DayOfWeek == date.EndOfWeek(firstDayOfWee |
| | | 328 | | |
| | | 329 | | /// <summary> |
| | | 330 | | /// Determines whether the current date falls on a weekend (Saturday or Sunday). |
| | | 331 | | /// </summary> |
| | 6 | 332 | | public bool IsWeekend() => date.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday; |
| | | 333 | | |
| | | 334 | | /// <summary> |
| | | 335 | | /// Returns the previous month keeping the time component intact. |
| | | 336 | | /// </summary> |
| | | 337 | | /// <returns>New date.</returns> |
| | | 338 | | public DateOnly PreviousMonth() |
| | | 339 | | { |
| | 9 | 340 | | var year = date.Month == 1 ? date.Year - 1 : date.Year; |
| | | 341 | | |
| | 9 | 342 | | var month = date.Month == 1 ? 12 : date.Month - 1; |
| | | 343 | | |
| | 9 | 344 | | var firstDayOfPreviousMonth = new DateOnly(year, month, 1); |
| | | 345 | | |
| | 9 | 346 | | var lastDayOfPreviousMonth = firstDayOfPreviousMonth.EndOfMonth().Day; |
| | | 347 | | |
| | 9 | 348 | | var day = date.Day > lastDayOfPreviousMonth ? lastDayOfPreviousMonth : date.Day; |
| | | 349 | | |
| | 9 | 350 | | return firstDayOfPreviousMonth.SetDay(day); |
| | | 351 | | } |
| | | 352 | | |
| | | 353 | | /// <summary> |
| | | 354 | | /// Returns the next month keeping the time component intact. |
| | | 355 | | /// </summary> |
| | | 356 | | /// <returns>New date.</returns> |
| | | 357 | | public DateOnly NextMonth() |
| | | 358 | | { |
| | 9 | 359 | | var year = date.Month == 12 ? date.Year + 1 : date.Year; |
| | | 360 | | |
| | 9 | 361 | | var month = date.Month == 12 ? 1 : date.Month + 1; |
| | | 362 | | |
| | 9 | 363 | | var firstDayOfNextMonth = new DateOnly(year, month, 1); |
| | | 364 | | |
| | 9 | 365 | | var lastDayOfPreviousMonth = firstDayOfNextMonth.EndOfMonth().Day; |
| | | 366 | | |
| | 9 | 367 | | var day = date.Day > lastDayOfPreviousMonth ? lastDayOfPreviousMonth : date.Day; |
| | | 368 | | |
| | 9 | 369 | | return firstDayOfNextMonth.SetDay(day); |
| | | 370 | | } |
| | | 371 | | |
| | | 372 | | /// <summary> |
| | | 373 | | /// Determines whether the date corresponds to today's day (UTC based). |
| | | 374 | | /// </summary> |
| | 0 | 375 | | public bool IsToday() => date.Day == DateTime.UtcNow.Day; |
| | | 376 | | |
| | | 377 | | /// <summary> |
| | | 378 | | /// Determines whether the specified date falls in the same week as the current date. |
| | | 379 | | /// </summary> |
| | | 380 | | /// <param name="date1">Value to compare with.</param> |
| | | 381 | | /// <returns><c>true</c> if the specified date is within the same week; otherwise <c>false</c>.</returns> |
| | 0 | 382 | | public bool SameWeek(DateOnly date1) => date1.IsAfter(date.BeginningOfWeek()) && date1.IsBefore(date.EndOfWeek() |
| | | 383 | | |
| | | 384 | | /// <summary> |
| | | 385 | | /// Determines whether the specified date is in the same month as the current date. |
| | | 386 | | /// </summary> |
| | 6 | 387 | | public bool SameMonth(DateOnly otherDate) => date.Month == otherDate.Month && date.Year == otherDate.Year; |
| | | 388 | | |
| | | 389 | | /// <summary> |
| | | 390 | | /// Determines whether the specified date is in the same year as the current date. |
| | | 391 | | /// </summary> |
| | 3 | 392 | | public bool SameYear(DateOnly otherDate) => date.Year == otherDate.Year; |
| | | 393 | | |
| | | 394 | | /// <summary> |
| | | 395 | | /// Determines whether the specified date is in the same decade as the current date. |
| | | 396 | | /// </summary> |
| | 6 | 397 | | public bool SameDecade(DateOnly otherDate) => date.BeginningOfDecade() == otherDate.BeginningOfDecade(); |
| | | 398 | | |
| | | 399 | | /// <summary> |
| | | 400 | | /// Returns the absolute number of days between two dates. |
| | | 401 | | /// </summary> |
| | 6 | 402 | | public int NumberOfDays(DateOnly dateTo) => Math.Abs((date.ToDateTime(TimeOnly.MinValue) - dateTo.ToDateTime(Tim |
| | | 403 | | |
| | | 404 | | /// <summary> |
| | | 405 | | /// Returns the difference in weeks between two dates (signed integer). |
| | | 406 | | /// </summary> |
| | | 407 | | public int CompareWeek(DateOnly dateTo) |
| | | 408 | | { |
| | 3 | 409 | | var ts = dateTo.ToDateTime(TimeOnly.MinValue).Subtract(date.ToDateTime(TimeOnly.MinValue)); |
| | 3 | 410 | | return ts.Days / 7; |
| | | 411 | | } |
| | | 412 | | |
| | | 413 | | /// <summary> |
| | | 414 | | /// Returns the absolute number of weeks between two dates. |
| | | 415 | | /// </summary> |
| | 3 | 416 | | public int NumberOfWeeks(DateOnly dateTo) => Math.Abs(date.CompareWeek(dateTo)); |
| | | 417 | | |
| | | 418 | | /// <summary> |
| | | 419 | | /// Compare two dates and return difference of months (signed integer). |
| | | 420 | | /// </summary> |
| | 9 | 421 | | public int CompareMonth(DateTime dateTo) => ((dateTo.Year - date.Year) * 12) + (dateTo.Month - date.Month); |
| | | 422 | | |
| | | 423 | | /// <summary> |
| | | 424 | | /// Returns the absolute number of months between two dates. |
| | | 425 | | /// </summary> |
| | 3 | 426 | | public int NumberOfMonths(DateTime dateTo) => Math.Abs(date.CompareMonth(dateTo)); |
| | | 427 | | |
| | | 428 | | /// <summary> |
| | | 429 | | /// Compare two dates and return difference of years (signed integer). |
| | | 430 | | /// </summary> |
| | 3 | 431 | | public int CompareYear(DateTime dateTo) => dateTo.Year - date.Year; |
| | | 432 | | |
| | | 433 | | /// <summary> |
| | | 434 | | /// Returns the absolute number of years between two dates. |
| | | 435 | | /// </summary> |
| | 3 | 436 | | public int NumberOfYears(DateTime dateTo) => Math.Abs(date.CompareYear(dateTo)); |
| | | 437 | | |
| | | 438 | | /// <summary> |
| | | 439 | | /// Calculates the age in years from a birthdate to now (UTC based). |
| | | 440 | | /// </summary> |
| | | 441 | | /// <returns>The number of full years elapsed since <paramref name="date"/>.</returns> |
| | | 442 | | public int Age() |
| | | 443 | | { |
| | 0 | 444 | | var today = DateTime.UtcNow; |
| | | 445 | | |
| | 0 | 446 | | var age = today.Year - date.Year; |
| | | 447 | | |
| | 0 | 448 | | if (today.Month < date.Month || (today.Month == date.Month && today.Day < date.Day)) |
| | 0 | 449 | | age--; |
| | | 450 | | |
| | 0 | 451 | | return age; |
| | | 452 | | } |
| | | 453 | | |
| | | 454 | | /// <summary> |
| | | 455 | | /// Generates a sequence of DateOnly values starting from a minimum value and ending at a maximum value, with a spec |
| | | 456 | | /// </summary> |
| | | 457 | | /// <param name="max">The maximum value of the range.</param> |
| | | 458 | | /// <param name="step">The step value for each iteration.</param> |
| | | 459 | | /// <param name="unit">The time unit for the step.</param> |
| | | 460 | | /// <returns>A sequence of DateOnly values within the specified range.</returns> |
| | | 461 | | public IEnumerable<DateOnly> Range(DateOnly max, int step = 1, TimeUnit unit = TimeUnit.Day) |
| | | 462 | | { |
| | 21 | 463 | | ArgumentOutOfRangeException.ThrowIfZero(step); |
| | | 464 | | |
| | 18 | 465 | | Func<DateOnly, DateOnly> increment = unit switch |
| | 18 | 466 | | { |
| | 6 | 467 | | TimeUnit.Day => x => x.AddDays(step), |
| | 3 | 468 | | TimeUnit.Week => x => x.AddDays(step * 7), |
| | 3 | 469 | | TimeUnit.Month => x => x.AddMonths(step), |
| | 3 | 470 | | TimeUnit.Year => x => x.AddYears(step), |
| | 0 | 471 | | TimeUnit.Millisecond => throw new InvalidOperationException(), |
| | 0 | 472 | | TimeUnit.Second => throw new InvalidOperationException(), |
| | 0 | 473 | | TimeUnit.Minute => throw new InvalidOperationException(), |
| | 3 | 474 | | TimeUnit.Hour => throw new InvalidOperationException(), |
| | 0 | 475 | | _ => x => x.AddDays(step) |
| | 18 | 476 | | }; |
| | | 477 | | |
| | 15 | 478 | | if (step > 0) |
| | | 479 | | { |
| | 132 | 480 | | for (var i = date; i <= max; i = increment(i)) |
| | 54 | 481 | | yield return i; |
| | | 482 | | } |
| | | 483 | | else |
| | | 484 | | { |
| | 36 | 485 | | for (var i = date; i >= max; i = increment(i)) |
| | 15 | 486 | | yield return i; |
| | | 487 | | } |
| | 15 | 488 | | } |
| | | 489 | | } |
| | | 490 | | } |
| | | 491 | | |