Skip to content

Commit 2dfea79

Browse files
committed
Add some new DateTime helpers
1 parent 9ee012c commit 2dfea79

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

SharpHelpers/SharpHelpers/DateTimeHelper.cs

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// (c) 2019 SharpCoding
22
// This code is licensed under MIT license (see LICENSE.txt for details)
33
using System;
4-
using System.Globalization;
5-
4+
using System.Globalization;
5+
66
namespace SharpCoding.SharpHelpers
77
{
88
public static class DateTimeHelper
@@ -46,56 +46,56 @@ public static DateTime AbsoluteEnd(this DateTime dateTime)
4646
public static DateTime? ToUniversalTime(this DateTime? dateTime)
4747
{
4848
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);
5949
}
6050

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>
6555
/// <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);
7059
}
7160

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>
7765
/// <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);
8770
}
8871

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>
9577
/// <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;
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;
9999
}
100100

101101
/// <summary>

0 commit comments

Comments
 (0)