欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net下日期和时间处理的类库

程序员文章站 2024-03-06 09:27:43
复制代码 代码如下: using system; namespace utilities { /// /// common datetime...
复制代码 代码如下:

using system;
namespace utilities
{
/// <summary>
/// common datetime methods.
/// </summary>
///
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 class dateutilities
{
#region quarter
public static datetime getstartofquarter( int year, quarter qtr )
{
if( qtr == quarter.first ) // 1st quarter = january 1 to march 31
return new datetime( year, 1, 1, 0, 0, 0, 0 );
else if( qtr == quarter.second ) // 2nd quarter = april 1 to june 30
return new datetime( year, 4, 1, 0, 0, 0, 0 );
else if( qtr == quarter.third ) // 3rd quarter = july 1 to september 30
return new datetime( year, 7, 1, 0, 0, 0, 0 );
else // 4th quarter = october 1 to december 31
return 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 new datetime( year, 12, datetime.daysinmonth( year, 12 ), 23, 59, 59, 999 );
}
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 year
return getendofquarter( datetime.now.year - 1, getquarter( month.december ));
else //return last quarter of current year
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 of current year
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 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 getstartoflastmonth()
{
if( datetime.now.month == 1 )
return getstartofmonth( 12, 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( 12, 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 new datetime( year, 1, 1, 0, 0, 0, 0 );
}
public static datetime getendofyear( int year )
{
return new datetime( year, 12, datetime.daysinmonth( year, 12 ), 23, 59, 59, 999 );
}
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
}
}