Copy Code code as follows:
Using System;
Namespace Utilities
{
<summary>
Common DateTime Methods.
</summary>
///
Public enum Quarter
{
i = 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 class Dateutilities
{
#region Quarter
public static DateTime Getstartofquarter (int year, quarter Qtr)
{
if (Qtr = = Quarter.first)//1st quarter = January 1 to March 31
Return to New DateTime (year, 1, 1, 0, 0, 0, 0);
else if (Qtr = quarter.second)//2nd quarter = April 1 to June 30
Return to New DateTime (year, 4, 1, 0, 0, 0, 0);
else if (Qtr = quarter.third)//3rd quarter = July 1 to September 30
Return to New DateTime (year, 7, 1, 0, 0, 0, 0);
else//4th quarter = October 1 to December 31
Return to New DateTime (year, 10, 1, 0, 0, 0, 0);
}
public static DateTime Getendofquarter (int year, quarter Qtr)
{
if (Qtr = = Quarter.first)//1st quarter = January 1 to March 31
return new DateTime (year, 3, Datetime.daysinmonth (year, 3), 23, 59, 59, 999);
else if (Qtr = quarter.second)//2nd quarter = April 1 to June 30
return new DateTime (year, 6, Datetime.daysinmonth (year, 6), 23, 59, 59, 999);
else if (Qtr = quarter.third)//3rd quarter = July 1 to September 30
return new DateTime (year, 9, Datetime.daysinmonth (year, 9), 23, 59, 59, 999);
else//4th quarter = October 1 to December 31
Return to New DateTime (year, 12), 23, 59, 59, 999 datetime.daysinmonth;
}
public static Quarter Getquarter (Month Month)
{
if (month <= month.march)//1st quarter = January 1 to March 31
return quarter.first;
else if ((month >= month.april) && (month <= month.june))//2nd quarter = April 1 to June 30
return quarter.second;
else if ((month >= month.july) && (month <= month.september))//3rd quarter = July 1 to September 30
return quarter.third;
else//4th quarter = October 1 to December 31
return Quarter.fourth;
}
public static DateTime Getendoflastquarter ()
{
if (DateTime.Now.Month <= (int) month.march)//go to last quarter of previous
Return Getendofquarter (Datetime.now.year-1, Getquarter (Month.december));
Else//return last quarter
Return Getendofquarter (DateTime.Now.Year, Getquarter (Month) DateTime.Now.Month));
}
public static DateTime Getstartoflastquarter ()
{
if (DateTime.Now.Month <= 3)//go to last quarter of previous year
Return Getstartofquarter (Datetime.now.year-1, Getquarter (Month.december));
Else//return last quarter
Return Getstartofquarter (DateTime.Now.Year, Getquarter (Month) DateTime.Now.Month));
}
public static DateTime Getstartofcurrentquarter ()
{
Return Getstartofquarter (DateTime.Now.Year, Getquarter (Month) DateTime.Now.Month));
}
public static DateTime Getendofcurrentquarter ()
{
Return Getendofquarter (DateTime.Now.Year, Getquarter (Month) DateTime.Now.Month));
}
#endregion
#region Weeks
public static DateTime Getstartoflastweek ()
{
int daystosubtract = (int) DateTime.Now.DayOfWeek + 7;
DateTime dt = DateTime.Now.Subtract (System.TimeSpan.FromDays (daystosubtract));
return new DateTime (dt. Year, dt. Month, dt. Day, 0, 0, 0, 0);
}
public static DateTime Getendoflastweek ()
{
DateTime dt = Getstartoflastweek (). AddDays (6);
return new DateTime (dt. Year, dt. Month, dt. Day, 23, 59, 59, 999);
}
public static DateTime Getstartofcurrentweek ()
{
int daystosubtract = (int) DateTime.Now.DayOfWeek;
DateTime dt = DateTime.Now.Subtract (System.TimeSpan.FromDays (daystosubtract));
return new DateTime (dt. Year, dt. Month, dt. Day, 0, 0, 0, 0);
}
public static DateTime Getendofcurrentweek ()
{
DateTime dt = Getstartofcurrentweek (). AddDays (6);
return new DateTime (dt. Year, dt. Month, dt. Day, 23, 59, 59, 999);
}
#endregion
#region Months
public static DateTime getstartofmonth (int Month, int)
{
Return to New DateTime (year, Month, 1, 0, 0, 0, 0);
}
public static DateTime getendofmonth (int Month, int)
{
Return to New DateTime (year, Month, Datetime.daysinmonth (year, Month), 23, 59, 59, 999);
}
public static DateTime Getstartoflastmonth ()
{
if (DateTime.Now.Month = 1)
Return Getstartofmonth (DATETIME.NOW.YEAR-1);
Else
Return Getstartofmonth (Datetime.now.month-1, DateTime.Now.Year);
}
public static DateTime Getendoflastmonth ()
{
if (DateTime.Now.Month = 1)
Return Getendofmonth (DATETIME.NOW.YEAR-1);
Else
Return Getendofmonth (Datetime.now.month-1, DateTime.Now.Year);
}
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
#region Years
public static DateTime getstartofyear (int year)
{
Return to New DateTime (year, 1, 1, 0, 0, 0, 0);
}
public static DateTime getendofyear (int year)
{
Return to New DateTime (year, 12), 23, 59, 59, 999 datetime.daysinmonth;
}
public static DateTime Getstartoflastyear ()
{
Return Getstartofyear (DATETIME.NOW.YEAR-1);
}
public static DateTime Getendoflastyear ()
{
Return Getendofyear (DATETIME.NOW.YEAR-1);
}
public static DateTime Getstartofcurrentyear ()
{
Return Getstartofyear (DateTime.Now.Year);
}
public static DateTime Getendofcurrentyear ()
{
Return Getendofyear (DateTime.Now.Year);
}
#endregion
#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
}
}