-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateTimeExtensions.cs
69 lines (60 loc) · 2.32 KB
/
DateTimeExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace BusinessDayCounting
{
public static class DateTimeExtensions
{
/// <summary>
/// List of weekend days, for extensibility and maintainability, we will put these in a static variable.
/// </summary>
private static readonly DayOfWeek[] WeekendDays = { DayOfWeek.Saturday, DayOfWeek.Sunday };
/// <summary>
/// check if a day is a weekend
/// </summary>
/// <param name="date">the date to check</param>
/// <returns>True if the given date is a weekend, otherwise False</returns>
public static bool IsWeekend(this DateTime date)
{
return WeekendDays.Any(weekendDay => weekendDay == date.DayOfWeek);
}
/// <summary>
/// check if a day is a business day (not a weekend day)
/// </summary>
/// <param name="date">the date to check</param>
/// <returns>True if the given date is a business day, otherwise False</returns>
public static bool IsBusinessDay(this DateTime date)
{
return (date.IsWeekend() == false);
}
public static DateTime FirstDayOfTheMonth(this DateTime date)
{
return new DateTime(date.Year, date.Month, 1);
}
public static DateTime LastDayOfTheMonth(this DateTime date)
{
return new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
}
/// <summary>
/// Get a list of dates in a month
/// </summary>
public static IEnumerable<DateTime> AllDaysInTheMonth(this DateTime date)
{
var firstOfMonth = date.FirstDayOfTheMonth();
var lastOfMonth = date.LastDayOfTheMonth();
for (var rollingDate = firstOfMonth; rollingDate <= lastOfMonth; rollingDate = rollingDate.AddDays(1))
{
yield return rollingDate.Date;
}
}
public static int WeekOfMonth(this DateTime date)
{
return (date.Day - 1) / 7 + 1;
}
public static int WeekOfYear(this DateTime date)
{
return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
}
}
}