MomentJS style .NET DateTime extensions
public static class DateTimeExtensions { | |
#region Day | |
public static DateTime StartOfDay(this DateTime d) { | |
return d.Date; | |
} | |
public static DateTime EndOfDay(this DateTime d) { | |
return new DateTime(d.Year, d.Month, d.Day, 23, 59, 59); | |
} | |
#endregion | |
#region Week | |
public static DateTime AddWeeks(this DateTime d, double weeks) { | |
return d.AddDays(weeks * 7); | |
} | |
public static DateTime StartOfWeek(this DateTime d) { | |
return d.AddDays(0 - (int)d.DayOfWeek).Date; | |
} | |
public static DateTime EndOfWeek(this DateTime d) { | |
return d.AddDays(6 - ((int)d.DayOfWeek)).Date; | |
} | |
#endregion | |
#region Month | |
public static DateTime StartOfMonth(this DateTime d) { | |
return new DateTime(d.Year, d.Month, 1).Date; | |
} | |
public static DateTime EndOfMonth(this DateTime d) { | |
return new DateTime(d.Year, d.Month, CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(d.Year, d.Month)).Date; | |
} | |
#endregion | |
#region IsSame | |
/// <summary> | |
/// Compares the two given DateTimes based on the given | |
/// comparison. | |
/// | |
/// The comparison also checks each "higher level" property. | |
/// For example if you are using <code>DateTimeComparison.Month</code> | |
/// the year will also be compared. | |
/// </summary> | |
public static bool IsSame(this DateTime a, DateTime b, DateTimeComparison comparison) { | |
if (a.Year != b.Year) return false; | |
if (comparison == DateTimeComparison.Year) return true; | |
if (a.Month != b.Month) return false; | |
if (comparison == DateTimeComparison.Month) return true; | |
if (a.Day != b.Day) return false; | |
if (comparison == DateTimeComparison.Day) return true; | |
if (a.Hour != b.Hour) return false; | |
if (comparison == DateTimeComparison.Hour) return true; | |
if (a.Minute != b.Minute) return false; | |
if (comparison == DateTimeComparison.Minute) return true; | |
return a.Second == b.Second; | |
} | |
#endregion | |
#region IsCurrent | |
/// <summary> | |
/// Compares the given <see cref="DateTime"/> to the current date and | |
/// time based on the given comparison. | |
/// | |
/// The comparison also checks each "higher level" property. | |
/// For example if you are using <code>DateTimeComparison.Month</code> | |
/// the year will also be compared. | |
/// </summary> | |
public static bool IsCurrent(this DateTime a, DateTimeComparison comparison) { | |
return IsSame(a, DateTime.Now, comparison); | |
} | |
#endregion | |
#region IsTomorrow | |
/// <summary> | |
/// Determines whether the given DateTime represents | |
/// the day after the current date (using <c>DateTime.Today</c>). | |
/// </summary> | |
public static bool IsTomorrow(this DateTime a) { | |
return IsSame(a, DateTime.Today.AddDays(1.0), DateTimeComparison.Day); | |
} | |
#endregion | |
#region IsYesterday | |
/// <summary> | |
/// Determines whether the given DateTime represents | |
/// the day before the current date (using <c>DateTime.Today</c>). | |
/// </summary> | |
public static bool IsYesterday(this DateTime a) { | |
return IsSame(a, DateTime.Today.AddDays(-1.0), DateTimeComparison.Day); | |
} | |
#endregion | |
#region IsBefore | |
/// <summary> | |
/// Returns true if the current <see cref="DateTime"/> represents | |
/// a point in time that occurs BEFORE the point in time represented | |
/// by <paramref name="b"/>. | |
/// | |
/// The comparison also checks each "higher level" property. | |
/// For example if you are using <code>DateTimeComparison.Month</code> | |
/// the year will also be compared. | |
/// </summary> | |
public static bool IsBefore(this DateTime a, DateTime b, DateTimeComparison comparison) { | |
if (a.Year < b.Year) return true; | |
if (a.Year > b.Year || comparison == DateTimeComparison.Year) return false; | |
if (a.Month < b.Month) return true; | |
if (a.Month > b.Month || comparison == DateTimeComparison.Month) return false; | |
if (a.Day < b.Day) return true; | |
if (a.Day > b.Day || comparison == DateTimeComparison.Day) return false; | |
if (a.Hour < b.Hour) return true; | |
if (a.Hour > b.Hour || comparison == DateTimeComparison.Hour) return false; | |
if (a.Minute < b.Minute) return true; | |
if (a.Minute > b.Minute || comparison == DateTimeComparison.Minute) return false; | |
return a.Second < b.Second; | |
} | |
#endregion | |
#region IsAfter | |
/// <summary> | |
/// Returns true if the current <see cref="DateTime"/> represents | |
/// a point in time that occurs AFTER the point in time represented | |
/// by <paramref name="b"/>. | |
/// | |
/// The comparison also checks each "higher level" property. | |
/// For example if you are using <code>DateTimeComparison.Month</code> | |
/// the year will also be compared. | |
/// </summary> | |
public static bool IsAfter(this DateTime a, DateTime b, DateTimeComparison comparison) { | |
if (a.Year > b.Year) return true; | |
if (a.Year < b.Year || comparison == DateTimeComparison.Year) return false; | |
if (a.Month > b.Month) return true; | |
if (a.Month < b.Month || comparison == DateTimeComparison.Month) return false; | |
if (a.Day > b.Day) return true; | |
if (a.Day < b.Day || comparison == DateTimeComparison.Day) return false; | |
if (a.Hour > b.Hour) return true; | |
if (a.Hour < b.Hour || comparison == DateTimeComparison.Hour) return false; | |
if (a.Minute > b.Minute) return true; | |
if (a.Minute < b.Minute || comparison == DateTimeComparison.Minute) return false; | |
return a.Second > b.Second; | |
} | |
#endregion | |
#region IsBetween | |
public static bool IsBetween(this DateTime a, DateTime startDate, DateTime endDate, DateTimeComparison comparison) { | |
double startDiff = (a - startDate).TotalSeconds; | |
double endDiff = (endDate - a).TotalSeconds; | |
if (comparison == DateTimeComparison.Second) { | |
return 0 <= startDiff && 0 <= endDiff; | |
} | |
// Minute | |
startDiff = Math.Round(startDiff/60); | |
endDiff = Math.Round(endDiff/60); | |
if (comparison == DateTimeComparison.Minute) { | |
return 0 <= startDiff && 0 <= endDiff; | |
} | |
// Hour | |
startDiff = Math.Round(startDiff / 60); | |
endDiff = Math.Round(endDiff / 60); | |
if (comparison == DateTimeComparison.Hour) { | |
return 0 <= startDiff && 0 <= endDiff; | |
} | |
// Day | |
startDiff = Math.Round(startDiff / 24); | |
endDiff = Math.Round(endDiff / 24); | |
if (comparison == DateTimeComparison.Day) { | |
return 0 <= startDiff && 0 <= endDiff; | |
} | |
throw new NotSupportedException("Cannot diff DateTimeComparison.Month or DateTimeComparison.Year"); | |
} | |
#endregion | |
} | |
public enum DateTimeComparison { | |
Year, | |
Month, | |
Day, | |
Hour, | |
Minute, | |
Second | |
} | |
public static class TimeSpanExtensions { | |
/// <summary> | |
/// Converts the given TimeSpan to a date time | |
/// using the Unix epoch for the year, month and day. | |
/// </summary> | |
public static DateTime ToDateTime(this TimeSpan ts) { | |
return ts.ToDateTime(new DateTime(1970, 1, 1)); | |
} | |
/// <summary> | |
/// Converts the given TimeSpan to a date time | |
/// using the given DateTime for the year, month and day. | |
/// </summary> | |
public static DateTime ToDateTime(this TimeSpan ts, DateTime d) { | |
if (ts.TotalDays >= 1) { | |
throw new ArgumentOutOfRangeException("ts", "The given TimeSpan cannot represent a duration greater than a day."); | |
} | |
return DateTimeHelper.FromDateAndTime(d, ts); | |
} | |
} | |
public static class DateTimeHelper { | |
/// <summary> | |
/// Creates a new date using the given <paramref name="date"/> | |
/// for the date (ie. a day) and the given <paramref name="time"/> | |
/// as the time (eg. 09:00). | |
/// </summary> | |
/// <param name="d">The date.</param> | |
/// <param name="ts">The time.</param> | |
/// <returns>A newly formed date time.</returns> | |
public static DateTime FromDateAndTime(DateTime d, TimeSpan ts) { | |
return new DateTime(d.Year, d.Month, d.Day, ts.Hours, ts.Minutes, ts.Seconds); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment