復制代碼 代碼如下:
using System;
namespace Utilities
{
/// <summary>
/// Common DateTime Methods
/// </summary>
///
public enum Quarter
{
First =
Second =
Third =
Fourth =
}
public enum Month
{
January =
February =
March =
April =
May =
June =
July =
August =
September =
October =
November =
December =
}
public class DateUtilities
{
#region Quarter
public static DateTime GetStartOfQuarter( int Year
{
if( Qtr == Quarter
return new DateTime( Year
else if( Qtr == Quarter
return new DateTime( Year
else if( Qtr == Quarter
return new DateTime( Year
else //
return new DateTime( Year
}
public static DateTime GetEndOfQuarter( int Year
{
if( Qtr == Quarter
return new DateTime( Year
else if( Qtr == Quarter
return new DateTime( Year
else if( Qtr == Quarter
return new DateTime( Year
else //
return new DateTime( Year
}
public static Quarter GetQuarter( Month month )
{
if( month <= Month
return Quarter
else if( ( month >= Month
return Quarter
else if( ( month >= Month
return Quarter
else //
return Quarter
}
public static DateTime GetEndOfLastQuarter()
{
if( DateTime
return GetEndOfQuarter( DateTime
else //return last quarter of current year
return GetEndOfQuarter( DateTime
}
public static DateTime GetStartOfLastQuarter()
{
if( DateTime
return GetStartOfQuarter( DateTime
else //return last quarter of current year
return GetStartOfQuarter( DateTime
}
public static DateTime GetStartOfCurrentQuarter()
{
return GetStartOfQuarter( DateTime
}
public static DateTime GetEndOfCurrentQuarter()
{
return GetEndOfQuarter( DateTime
}
#endregion
#region Weeks
public static DateTime GetStartOfLastWeek()
{
int DaysToSubtract = (int)DateTime
DateTime dt = DateTime
return new DateTime( dt
}
public static DateTime GetEndOfLastWeek()
{
DateTime dt = GetStartOfLastWeek()
return new DateTime( dt
}
public static DateTime GetStartOfCurrentWeek()
{
int DaysToSubtract = (int)DateTime
DateTime dt = DateTime
return new DateTime( dt
}
public static DateTime GetEndOfCurrentWeek()
{
DateTime dt = GetStartOfCurrentWeek()
return new DateTime( dt
}
#endregion
#region Months
public static DateTime GetStartOfMonth( int Month
{
return new DateTime( Year
}
public static DateTime GetEndOfMonth( int Month
{
return new DateTime( Year
}
public static DateTime GetStartOfLastMonth()
{
if( DateTime
return GetStartOfMonth(
else
return GetStartOfMonth( DateTime
}
public static DateTime GetEndOfLastMonth()
{
if( DateTime
return GetEndOfMonth(
else
return GetEndOfMonth( DateTime
}
public static DateTime GetStartOfCurrentMonth()
{
return GetStartOfMonth( DateTime
}
public static DateTime GetEndOfCurrentMonth()
{
return GetEndOfMonth( DateTime
}
#endregion
#region Years
public static DateTime GetStartOfYear( int Year )
{
return new DateTime( Year
}
public static DateTime GetEndOfYear( int Year )
{
return new DateTime( Year
}
public static DateTime GetStartOfLastYear()
{
return GetStartOfYear( DateTime
}
public static DateTime GetEndOfLastYear()
{
return GetEndOfYear( DateTime
}
public static DateTime GetStartOfCurrentYear()
{
return GetStartOfYear( DateTime
}
public static DateTime GetEndOfCurrentYear()
{
return GetEndOfYear( DateTime
}
#endregion
#region Days
public static DateTime GetStartOfDay( DateTime date )
{
return new DateTime( date
}
public static DateTime GetEndOfDay( DateTime date )
{
return new DateTime( date
}
#endregion
}
}
From:http://tw.wingwit.com/Article/program/net/201404/30515.html