熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

asp.net下日期和時間處理的類庫

2022-06-13   來源: .NET編程 

  復制代碼 代碼如下:

  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 Quarter Qtr )
{
if( Qtr == QuarterFirst ) // st Quarter = January to March
return new DateTime( Year );
else if( Qtr == QuarterSecond ) // nd Quarter = April to June
return new DateTime( Year );
else if( Qtr == QuarterThird ) // rd Quarter = July to September
return new DateTime( Year );
else // th Quarter = October to December
return new DateTime( Year );
}
public static DateTime GetEndOfQuarter( int Year Quarter Qtr )
{
if( Qtr == QuarterFirst ) // st Quarter = January to March
return new DateTime( Year DateTimeDaysInMonth( Year ) );
else if( Qtr == QuarterSecond ) // nd Quarter = April to June
return new DateTime( Year DateTimeDaysInMonth( Year ) );
else if( Qtr == QuarterThird ) // rd Quarter = July to September
return new DateTime( Year DateTimeDaysInMonth( Year ) );
else // th Quarter = October to December
return new DateTime( Year DateTimeDaysInMonth( Year ) );
}
public static Quarter GetQuarter( Month month )
{
if( month <= MonthMarch ) // st Quarter = January to March
return QuarterFirst;
else if( ( month >= MonthApril ) && ( month <= MonthJune ) ) // nd Quarter = April to June
return QuarterSecond;
else if( ( month >= MonthJuly ) && ( month <= MonthSeptember ) ) // rd Quarter = July to September
return QuarterThird;
else // th Quarter = October to December
return QuarterFourth;
}
public static DateTime GetEndOfLastQuarter()
{
if( DateTimeNowMonth <= (int)MonthMarch ) //go to last quarter of previous year
return GetEndOfQuarter( DateTimeNowYear GetQuarter( MonthDecember ));
else //return last quarter of current year
return GetEndOfQuarter( DateTimeNowYear GetQuarter( (Month)DateTimeNowMonth));
}
public static DateTime GetStartOfLastQuarter()
{
if( DateTimeNowMonth <= ) //go to last quarter of previous year
return GetStartOfQuarter( DateTimeNowYear GetQuarter( MonthDecember ));
else //return last quarter of current year
return GetStartOfQuarter( DateTimeNowYear GetQuarter( (Month)DateTimeNowMonth));
}
public static DateTime GetStartOfCurrentQuarter()
{
return GetStartOfQuarter( DateTimeNowYear GetQuarter( (Month)DateTimeNowMonth ));
}
public static DateTime GetEndOfCurrentQuarter()
{
return GetEndOfQuarter( DateTimeNowYear GetQuarter( (Month)DateTimeNowMonth ));
}
#endregion
#region Weeks
public static DateTime GetStartOfLastWeek()
{
int DaysToSubtract = (int)DateTimeNowDayOfWeek + ;
DateTime dt = DateTimeNowSubtract( SystemTimeSpanFromDays( DaysToSubtract ) );
return new DateTime( dtYear dtMonth dtDay );
}
public static DateTime GetEndOfLastWeek()
{
DateTime dt = GetStartOfLastWeek()AddDays();
return new DateTime( dtYear dtMonth dtDay );
}
public static DateTime GetStartOfCurrentWeek()
{
int DaysToSubtract = (int)DateTimeNowDayOfWeek ;
DateTime dt = DateTimeNowSubtract( SystemTimeSpanFromDays( DaysToSubtract ) );
return new DateTime( dtYear dtMonth dtDay );
}
public static DateTime GetEndOfCurrentWeek()
{
DateTime dt = GetStartOfCurrentWeek()AddDays();
return new DateTime( dtYear dtMonth dtDay );
}
#endregion
#region Months
public static DateTime GetStartOfMonth( int Month int Year )
{
return new DateTime( Year Month );
}
public static DateTime GetEndOfMonth( int Month int Year )
{
return new DateTime( Year Month DateTimeDaysInMonth( Year Month ) );
}
public static DateTime GetStartOfLastMonth()
{
if( DateTimeNowMonth == )
return GetStartOfMonth( DateTimeNowYear );
else
return GetStartOfMonth( DateTimeNowMonth DateTimeNowYear );
}
public static DateTime GetEndOfLastMonth()
{
if( DateTimeNowMonth == )
return GetEndOfMonth( DateTimeNowYear );
else
return GetEndOfMonth( DateTimeNowMonth DateTimeNowYear );
}
public static DateTime GetStartOfCurrentMonth()
{
return GetStartOfMonth( DateTimeNowMonth DateTimeNowYear );
}
public static DateTime GetEndOfCurrentMonth()
{
return GetEndOfMonth( DateTimeNowMonth DateTimeNowYear );
}
#endregion
#region Years
public static DateTime GetStartOfYear( int Year )
{
return new DateTime( Year );
}
public static DateTime GetEndOfYear( int Year )
{
return new DateTime( Year DateTimeDaysInMonth( Year ) );
}
public static DateTime GetStartOfLastYear()
{
return GetStartOfYear( DateTimeNowYear );
}
public static DateTime GetEndOfLastYear()
{
return GetEndOfYear( DateTimeNowYear );
}
public static DateTime GetStartOfCurrentYear()
{
return GetStartOfYear( DateTimeNowYear );
}
public static DateTime GetEndOfCurrentYear()
{
return GetEndOfYear( DateTimeNowYear );
}
#endregion
#region Days
public static DateTime GetStartOfDay( DateTime date )
{
return new DateTime( dateYear dateMonth dateDay );
}
public static DateTime GetEndOfDay( DateTime date )
{
return new DateTime( dateYear dateMonth dateDay );
}
#endregion
}
}


From:http://tw.wingwit.com/Article/program/net/201404/30515.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.