Skip to content

Commit 9ee012c

Browse files
committed
Add some new DateTime helpers
1 parent 59f29fc commit 9ee012c

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// (c) 2023 SharpCoding
2+
// This code is licensed under MIT license (see LICENSE.txt for details)
3+
4+
using System;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using SharpCoding.SharpHelpers;
7+
using System.Linq;
8+
9+
namespace SharpHelpers.UnitTest.DateAndTime
10+
{
11+
[TestClass]
12+
public class DateTimeTest
13+
{
14+
[TestMethod]
15+
public void TestDateTimeSmart()
16+
{
17+
var dtDictionary = new System.Collections.Generic.Dictionary<string, string>()
18+
{
19+
{"M/dd/yyyy hh:mm", "10/11/2023 09:00"},
20+
{"MM/dd/yyyy hh:mm:ss tt zzz", "05/01/2009 01:30:42 PM -05:00"},
21+
{"g", "10/11/2023 09:00"}
22+
};
23+
24+
DateTimeSmart.AddFormats(dtDictionary.Keys.ToArray());
25+
26+
foreach (var key in dtDictionary.Keys)
27+
{
28+
DateTime dt = (DateTimeSmart)dtDictionary[key];
29+
Assert.IsFalse(dt == DateTime.MinValue);
30+
}
31+
32+
DateTime dt1 = (DateTimeSmart)"01-05-2023 22:30:55";
33+
Assert.IsTrue(dt1 == new DateTime( 2023, 5, 1, 22, 30, 55, DateTimeKind.Local));
34+
}
35+
36+
[TestMethod]
37+
public void IsLeapYear_ShouldReturnTrueForLeapYear()
38+
{
39+
var leapYear = new DateTime(2024, 1, 1);
40+
var isLeapYear = leapYear.IsLeapYear();
41+
42+
Assert.IsTrue(isLeapYear);
43+
}
44+
45+
[TestMethod]
46+
public void Age_ShouldCalculateCorrectAge()
47+
{
48+
var birthDate = new DateTime(1990, 8, 18);
49+
var today = new DateTime(2024, 8, 18);
50+
var expectedAge = 34;
51+
52+
var age = birthDate.Age();
53+
54+
Assert.AreEqual(expectedAge, age);
55+
}
56+
57+
[TestMethod]
58+
public void AddBusinessDays_ShouldSkipWeekends()
59+
{
60+
var startDate = new DateTime(2024, 8, 16); // Friday
61+
var expected = new DateTime(2024, 8, 20); // 2 business days later (Tuesday)
62+
63+
var result = startDate.AddBusinessDays(2);
64+
65+
Assert.AreEqual(expected, result);
66+
}
67+
68+
[TestMethod]
69+
public void IsWeekend_ShouldReturnFalseForWeekday()
70+
{
71+
var monday = new DateTime(2024, 8, 19);
72+
73+
var isMondayWeekend = monday.IsWeekend();
74+
75+
Assert.IsFalse(isMondayWeekend);
76+
}
77+
}
78+
}

SharpHelpers/SharpHelpers/DateTimeHelper.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,63 @@ public static bool IsBetween(this DateTime dt, DateTime rangeBeg, DateTime range
9797
{
9898
return dt.Ticks >= rangeBeg.Ticks && dt.Ticks <= rangeEnd.Ticks;
9999
}
100+
101+
/// <summary>
102+
/// Check if a day is a weekend.
103+
/// </summary>
104+
/// <param name="dateTime"></param>
105+
/// <returns></returns>
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);
157+
}
100158
}
101-
}
159+
}

0 commit comments

Comments
 (0)