Created
April 5, 2013 15:29
-
-
Save JustLikeIcarus/5320208 to your computer and use it in GitHub Desktop.
Extends System.DateTime to provide various utility functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace System | |
{ | |
public static class DateTimeExtensions | |
{ | |
/// <summary> | |
/// Returns the Week Number of the supplied date object | |
/// </summary> | |
/// <param name="value"></param> | |
/// <returns>int</returns> | |
public static int WeekNumber(this System.DateTime value) | |
{ | |
return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(value, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday); | |
} | |
/// <summary> | |
/// Returns total number of weeks since 1/1/1970 | |
/// </summary> | |
/// <param name="value"></param> | |
/// <returns></returns> | |
public static int EpochWeekNumber(this System.DateTime value) | |
{ | |
TimeSpan t = value - new DateTime(1970, 1, 1); | |
int secondsSinceEpoch = (int)t.TotalSeconds; | |
int weeksSinceEpoch = secondsSinceEpoch / 604800; | |
return weeksSinceEpoch; | |
} | |
/// <summary> | |
/// Returns a DateTime object for the last day of the supplied dates week. | |
/// </summary> | |
/// <param name="dateTime"></param> | |
/// <returns>Datetime</returns> | |
public static DateTime EndOfWeek(this DateTime dateTime) | |
{ | |
DateTime start = StartOfWeek(dateTime); | |
return start.AddDays(7).AddSeconds(-1); | |
} | |
/// <summary> | |
/// Returns a DateTime object for the first day of the supplied dates week. | |
/// </summary> | |
/// <param name="dateTime"></param> | |
/// <returns>DateTime</returns> | |
public static DateTime StartOfWeek(this DateTime dateTime) | |
{ | |
int days = dateTime.DayOfWeek - DayOfWeek.Sunday; | |
if (days < 0) | |
days += 7; | |
return dateTime.AddDays(-1 * days).Date; | |
} | |
/// <summary> | |
/// Returns a DateTime object for the first day of the supplied dates preivous week. | |
/// </summary> | |
/// <param name="dateTime"></param> | |
/// <returns></returns> | |
public static DateTime StartOfPreviousWeek(this DateTime dateTime) | |
{ | |
DateTime oneWeekAgo = dateTime.AddDays(-7); | |
return FirstDateOfWeek(oneWeekAgo.Year, oneWeekAgo.WeekNumber()); | |
} | |
/// <summary> | |
/// Returns beginning date for a provided week of year | |
/// </summary> | |
/// <param name="year"></param> | |
/// <param name="weekOfYear"></param> | |
/// <returns></returns> | |
public static DateTime FirstDateOfWeek(int year, int weekOfYear) | |
{ | |
DateTime jan1 = new DateTime(year, 1, 1); | |
int daysOffset = (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek - (int)jan1.DayOfWeek; | |
DateTime firstMonday = jan1.AddDays(daysOffset); | |
int firstWeek = jan1.WeekNumber(); | |
if (firstWeek <= 1) | |
{ | |
weekOfYear -= 1; | |
} | |
return firstMonday.AddDays(weekOfYear * 7); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment