DayOfWeekExtensions.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Linq;
  3. namespace Abp.Extensions
  4. {
  5. /// <summary>
  6. /// Extension methods for <see cref="DayOfWeekExtensions"/>.
  7. /// </summary>
  8. public static class DayOfWeekExtensions
  9. {
  10. /// <summary>
  11. /// Check if a given <see cref="DayOfWeek"/> value is weekend.
  12. /// </summary>
  13. public static bool IsWeekend(this DayOfWeek dayOfWeek)
  14. {
  15. return dayOfWeek.IsIn(DayOfWeek.Saturday, DayOfWeek.Sunday);
  16. }
  17. /// <summary>
  18. /// Check if a given <see cref="DayOfWeek"/> value is weekday.
  19. /// </summary>
  20. public static bool IsWeekday(this DayOfWeek dayOfWeek)
  21. {
  22. return dayOfWeek.IsIn(DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday);
  23. }
  24. /// <summary>
  25. /// Finds the NTH week day of a month.
  26. /// </summary>
  27. /// <param name="dayOfWeek">The day of week.</param>
  28. /// <param name="year">The year.</param>
  29. /// <param name="month">The month.</param>
  30. /// <param name="n">The nth instance.</param>
  31. /// <remarks>Compensates for 4th and 5th DayOfWeek of Month</remarks>
  32. public static DateTime FindNthWeekDayOfMonth(this DayOfWeek dayOfWeek, int year, int month, int n)
  33. {
  34. if (n < 1 || n > 5)
  35. {
  36. throw new ArgumentOutOfRangeException(nameof(n));
  37. }
  38. var y = 0;
  39. var daysOfMonth = DateTimeExtensions.DaysOfMonth(year, month);
  40. // compensate for "last DayOfWeek in month"
  41. var totalInstances = dayOfWeek.TotalInstancesInMonth(year, month);
  42. if (n == 5 && n > totalInstances)
  43. n = 4;
  44. var foundDate = daysOfMonth
  45. .Where(date => dayOfWeek.Equals(date.DayOfWeek))
  46. .OrderBy(date => date)
  47. .Select(x => new { n = ++y, date = x })
  48. .Where(x => x.n.Equals(n)).Select(x => x.date).First(); //black magic wizardry
  49. return foundDate;
  50. }
  51. /// <summary>
  52. /// Finds the total number of instances of a specific DayOfWeek in a month.
  53. /// </summary>
  54. /// <param name="dayOfWeek">The day of week.</param>
  55. /// <param name="year">The year.</param>
  56. /// <param name="month">The month.</param>
  57. /// <returns></returns>
  58. public static int TotalInstancesInMonth(this DayOfWeek dayOfWeek, int year, int month)
  59. {
  60. return DateTimeExtensions.DaysOfMonth(year, month).Count(date => dayOfWeek.Equals(date.DayOfWeek));
  61. }
  62. /// <summary>
  63. /// Gets the total number of instances of a specific DayOfWeek in a month.
  64. /// </summary>
  65. /// <param name="dayOfWeek">The day of week.</param>
  66. /// <param name="dateTime">The date in a month.</param>
  67. /// <returns></returns>
  68. public static int TotalInstancesInMonth(this DayOfWeek dayOfWeek, DateTime dateTime)
  69. {
  70. return dayOfWeek.TotalInstancesInMonth(dateTime.Year, dateTime.Month);
  71. }
  72. }
  73. }