/// <summary> /// Returns the start of the year, month, week, day or hour for the given <see cref="DateTime"/> /// This implementation requires <see cref="CultureInfo"/> to be provided /// </summary> /// <param name="timeAnchor">Anchors the returned value to a starting point (year/month/week/day/hour)</param> /// <returns></returns> /// <exception cref="ArgumentException">Thrown when an invalid value is casted to <see cref="DateTimeAnchor"/>r</exception> public static DateTime StartOf(this DateTime This, DateTimeAnchor timeAnchor, CultureInfo cultureInfo) { switch (timeAnchor) { case DateTimeAnchor.Minute: return(new DateTime(This.Year, This.Month, This.Day, This.Hour, This.Minute, 0, 0)); case DateTimeAnchor.Hour: return(new DateTime(This.Year, This.Month, This.Day, This.Hour, 0, 0, 0)); case DateTimeAnchor.Day: return(new DateTime(This.Year, This.Month, This.Day, 0, 0, 0, 0)); case DateTimeAnchor.Week: var tmp = This.FirstDateInWeek(cultureInfo); return(new DateTime(tmp.Year, tmp.Month, tmp.Day, 0, 0, 0, 0)); case DateTimeAnchor.Month: return(new DateTime(This.Year, This.Month, 1, 0, 0, 0, 0)); case DateTimeAnchor.Year: return(new DateTime(This.Year, 1, 1, 0, 0, 0, 0)); default: throw new ArgumentException(); } }
/// <summary> /// Returns the end of a year, month, week, day or hour for the given <see cref="DateTime"/> /// This implementation requires a <see cref="CultureInfo"/> to be provided /// </summary> /// <param name="timeAnchor">Anchors the returned value to a starting point (year/month/week/day/hour)</param> /// <returns></returns> /// <exception cref="ArgumentException">Thrown when an invalid value is casted to timeAnchor</exception> public static DateTime EndOf(this DateTime This, DateTimeAnchor timeAnchor, CultureInfo cultureInfo) { switch (timeAnchor) { case DateTimeAnchor.Minute: return(new DateTime(This.Year, This.Month, This.Day, This.Hour, This.Minute, 59, 999)); case DateTimeAnchor.Hour: return(new DateTime(This.Year, This.Month, This.Day, This.Hour, 59, 59, 999)); case DateTimeAnchor.Day: return(new DateTime(This.Year, This.Month, This.Day, 23, 59, 59, 999)); case DateTimeAnchor.Week: var tmp = This.LastDateInWeek(cultureInfo); return(new DateTime(tmp.Year, tmp.Month, tmp.Day, 23, 59, 59, 999)); case DateTimeAnchor.Month: var days = DateTime.DaysInMonth(This.Year, This.Month); return(new DateTime(This.Year, This.Month, days, 23, 59, 59, 999)); case DateTimeAnchor.Year: return(new DateTime(This.Year, 12, DateTime.DaysInMonth(This.Year, 12), 23, 59, 59, 999)); default: throw new ArgumentException(); } }
/// <summary> /// Returns the start of the year, month, week, day or hour for the given <see cref="DateTime"/> /// This implementation uses the current culture /// </summary> /// <param name="timeAnchor">Anchors the returned value to a starting point (year/month/week/day/hour)</param> /// <returns></returns> /// <exception cref="ArgumentException">Thrown when an invalid value is casted to timeAnchor</exception> public static DateTime StartOf(this DateTime This, DateTimeAnchor timeAnchor) { return(This.StartOf(timeAnchor, CultureInfo.CurrentCulture)); }
/// <summary> /// Returns the end of a year, month, week, day or hour for the given <see cref="DateTime"/> /// This implementation uses the current culture /// </summary> /// <param name="timeAnchor">Anchors the returned value to a starting point (year/month/week/day/hour)</param> /// <returns></returns> /// <exception cref="InvalidCastException">Thrown when an invalid value is casted to <see cref="DateTimeAnchor"/></exception> public static DateTime EndOf(this DateTime This, DateTimeAnchor timeAnchor) => This.EndOf(timeAnchor, CultureInfo.CurrentCulture);