|
1 | 1 | // (c) 2019 SharpCoding
|
2 | 2 | // This code is licensed under MIT license (see LICENSE.txt for details)
|
3 | 3 | using System;
|
4 |
| -using System.Globalization; |
5 |
| - |
| 4 | +using System.Globalization; |
| 5 | + |
6 | 6 | namespace SharpCoding.SharpHelpers
|
7 | 7 | {
|
8 | 8 | public static class DateTimeHelper
|
@@ -46,56 +46,114 @@ public static DateTime AbsoluteEnd(this DateTime dateTime)
|
46 | 46 | public static DateTime? ToUniversalTime(this DateTime? dateTime)
|
47 | 47 | {
|
48 | 48 | return dateTime?.ToUniversalTime();
|
49 |
| - } |
50 |
| - |
51 |
| - /// <summary> |
52 |
| - /// Return the start of the current day |
53 |
| - /// </summary> |
54 |
| - /// <param name="dateTime"></param> |
55 |
| - /// <returns></returns> |
56 |
| - public static DateTime GetStartOfDay(this DateTime dateTime) |
57 |
| - { |
58 |
| - return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, 0); |
59 | 49 | }
|
60 | 50 |
|
61 |
| - /// <summary> |
62 |
| - /// Return the end of the current day |
63 |
| - /// </summary> |
64 |
| - /// <param name="dateTime"></param> |
| 51 | + /// <summary> |
| 52 | + /// Return the start of the current day |
| 53 | + /// </summary> |
| 54 | + /// <param name="dateTime"></param> |
65 | 55 | /// <returns></returns>
|
66 |
| - public static DateTime GetEndOfDay(this DateTime dateTime) |
67 |
| - { |
68 |
| - return new DateTime(dateTime.Year, dateTime.Month, |
69 |
| - dateTime.Day, 23, 59, 59, 999); |
| 56 | + public static DateTime GetStartOfDay(this DateTime dateTime) |
| 57 | + { |
| 58 | + return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, 0); |
70 | 59 | }
|
71 | 60 |
|
72 |
| - /// <summary> |
73 |
| - /// Try to parse the string value with specific DateTime formats |
74 |
| - /// </summary> |
75 |
| - /// <param name="value"></param> |
76 |
| - /// <param name="formats"></param> |
| 61 | + /// <summary> |
| 62 | + /// Return the end of the current day |
| 63 | + /// </summary> |
| 64 | + /// <param name="dateTime"></param> |
77 | 65 | /// <returns></returns>
|
78 |
| - public static DateTime? AsDateTime(this string value, string[] formats) |
79 |
| - { |
80 |
| - if (DateTime.TryParseExact(value, formats, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, |
81 |
| - out DateTime utcDateTime)) |
82 |
| - { |
83 |
| - return utcDateTime; |
84 |
| - } |
85 |
| - |
86 |
| - return null; |
| 66 | + public static DateTime GetEndOfDay(this DateTime dateTime) |
| 67 | + { |
| 68 | + return new DateTime(dateTime.Year, dateTime.Month, |
| 69 | + dateTime.Day, 23, 59, 59, 999); |
87 | 70 | }
|
88 | 71 |
|
89 |
| - /// <summary> |
90 |
| - /// Check if a date is between two dates |
91 |
| - /// </summary> |
92 |
| - /// <param name="dt"></param> |
93 |
| - /// <param name="rangeBeg"></param> |
94 |
| - /// <param name="rangeEnd"></param> |
| 72 | + /// <summary> |
| 73 | + /// Try to parse the string value with specific DateTime formats |
| 74 | + /// </summary> |
| 75 | + /// <param name="value"></param> |
| 76 | + /// <param name="formats"></param> |
| 77 | + /// <returns></returns> |
| 78 | + public static DateTime? AsDateTime(this string value, string[] formats) |
| 79 | + { |
| 80 | + if (DateTime.TryParseExact(value, formats, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, |
| 81 | + out DateTime utcDateTime)) |
| 82 | + { |
| 83 | + return utcDateTime; |
| 84 | + } |
| 85 | + |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Check if a date is between two dates |
| 91 | + /// </summary> |
| 92 | + /// <param name="dt"></param> |
| 93 | + /// <param name="rangeBeg"></param> |
| 94 | + /// <param name="rangeEnd"></param> |
| 95 | + /// <returns></returns> |
| 96 | + public static bool IsBetween(this DateTime dt, DateTime rangeBeg, DateTime rangeEnd) |
| 97 | + { |
| 98 | + return dt.Ticks >= rangeBeg.Ticks && dt.Ticks <= rangeEnd.Ticks; |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Check if a day is a weekend. |
| 103 | + /// </summary> |
| 104 | + /// <param name="dateTime"></param> |
95 | 105 | /// <returns></returns>
|
96 |
| - public static bool IsBetween(this DateTime dt, DateTime rangeBeg, DateTime rangeEnd) |
97 |
| - { |
98 |
| - return dt.Ticks >= rangeBeg.Ticks && dt.Ticks <= rangeEnd.Ticks; |
| 106 | + public static bool IsWeekend(this DateTime dateTime) |
| 107 | + { |
| 108 | + return dateTime.DayOfWeek == DayOfWeek.Saturday || dateTime.DayOfWeek == DayOfWeek.Sunday; |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Add business days to a DateTime. |
| 113 | + /// </summary> |
| 114 | + /// <param name="dateTime"></param> |
| 115 | + /// <param name="businessDays"></param> |
| 116 | + /// <returns></returns> |
| 117 | + public static DateTime AddBusinessDays(this DateTime dateTime, int businessDays) |
| 118 | + { |
| 119 | + if (businessDays == 0) |
| 120 | + return dateTime; |
| 121 | + |
| 122 | + int direction = businessDays > 0 ? 1 : -1; |
| 123 | + int daysToAdd = Math.Abs(businessDays); |
| 124 | + |
| 125 | + while (daysToAdd > 0) |
| 126 | + { |
| 127 | + dateTime = dateTime.AddDays(direction); |
| 128 | + if (!dateTime.IsWeekend()) |
| 129 | + daysToAdd--; |
| 130 | + } |
| 131 | + |
| 132 | + return dateTime; |
| 133 | + } |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Return the age of a person. |
| 137 | + /// </summary> |
| 138 | + /// <param name="birthDate"></param> |
| 139 | + /// <returns></returns> |
| 140 | + public static int Age(this DateTime birthDate) |
| 141 | + { |
| 142 | + var today = DateTime.Today; |
| 143 | + var age = today.Year - birthDate.Year; |
| 144 | + if (birthDate.Date > today.AddYears(-age)) age--; |
| 145 | + return age; |
| 146 | + } |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// Check if the year is a leap year. |
| 150 | + /// </summary> |
| 151 | + /// <param name="dateTime"></param> |
| 152 | + /// <returns></returns> |
| 153 | + public static bool IsLeapYear(this DateTime dateTime) |
| 154 | + { |
| 155 | + int year = dateTime.Year; |
| 156 | + return DateTime.IsLeapYear(year); |
99 | 157 | }
|
100 | 158 | }
|
101 | 159 | }
|
0 commit comments