Skip to content

Instantly share code, notes, and snippets.

@aruss
Created April 24, 2015 12:41
Show Gist options
  • Save aruss/4c44232b08873fd25d89 to your computer and use it in GitHub Desktop.
Save aruss/4c44232b08873fd25d89 to your computer and use it in GitHub Desktop.
DateUtilities.cs
using System;
using System.Globalization;
namespace eConduct.Extensions
{
public enum Quarter
{
First = 1,
Second = 2,
Third = 3,
Fourth = 4
}
public enum Month
{
January = 1,
February = 2,
March = 3,
April = 4,
May = 5,
June = 6,
July = 7,
August = 8,
September = 9,
October = 10,
November = 11,
December = 12
}
public static class DateTimeExtensions
{
private static readonly GregorianCalendar gc = new GregorianCalendar();
public static int GetWeekOfMonth(this DateTime date)
{
var first = new DateTime(date.Year, date.Month, 1);
return date.GetWeekOfYear() - first.GetWeekOfYear() + 1;
}
static int GetWeekOfYear(this DateTime date)
{
return DateTimeExtensions.gc.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
}
public static DateTime GetStartOfDay(this DateTime date)
{
return DateUtilities.GetStartOfDay(date);
}
public static DateTime GetEndOfDay(this DateTime date)
{
return DateUtilities.GetEndOfDay(date);
}
public static DateTime GetStartOfMonth(this DateTime date)
{
return DateUtilities.GetStartOfMonth(date.Month, date.Year);
}
public static DateTime GetEndOfMonth(this DateTime date)
{
return DateUtilities.GetEndOfMonth(date.Month, date.Year);
}
public static DateTime GetStartOfCurrentMonth(this DateTime date)
{
return DateUtilities.GetStartOfCurrentMonth();
}
public static DateTime GetEndOfCurrentMonth(this DateTime date)
{
return DateUtilities.GetEndOfCurrentMonth();
}
}
public class DateUtilities
{
#region Days
public static DateTime GetStartOfDay(DateTime date)
{
return new DateTime(date.Year, date.Month, date.Day, 0, 0, 0, 0);
}
public static DateTime GetEndOfDay(DateTime date)
{
return new DateTime(date.Year, date.Month, date.Day, 23, 59, 59, 999);
}
#endregion
#region Months
public static DateTime GetStartOfMonth(int month, int year)
{
return new DateTime(year, month, 1, 0, 0, 0, 0);
}
public static DateTime GetEndOfMonth(int month, int year)
{
return new DateTime(year, month, DateTime.DaysInMonth(year, month), 23, 59, 59, 999);
}
public static DateTime GetStartOfCurrentMonth()
{
return GetStartOfMonth(DateTime.Now.Month, DateTime.Now.Year);
}
public static DateTime GetEndOfCurrentMonth()
{
return GetEndOfMonth(DateTime.Now.Month, DateTime.Now.Year);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment