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

代码如下:

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
}
}

(0)

相关推荐

  • asp.net 时间类 一周的周一和周末的日期

    好多时候,数据库查询统计时需要使用本周统计 这里又不想把计算的任务交给数据去做 于是自己写个小函数,得到本周的周一和周末的时间,数据库比较的时候只需要 between ... and 就可以了 代码如下: #region 得到一周的周一和周日的日期 /// <summary> /// 计算本周的周一日期 /// </summary> /// <returns></returns> public static DateTime GetMondayDate()

  • JS 显示当前日期与时间的代码

    JS 显示当前日期 复制代码 代码如下: <script language="Javascript"> var datelocalweek=new Array("星期日", "星期一", "星期二","星期三","星期四", "星期五","星期六"); var datelocalnow=new Date(); var datelocal

  • .Net与JS时间日期格式的转换问题对比分析

    本文实例分析了.Net与JS时间日期格式的转换问题.分享给大家供大家参考,具体如下: Js中的1415349957524整数 ,其实代表的是1970.1.1 00:00:00至现今某个时间点的时间间隔毫秒数.而在.Net中,我们可以用Ticks属性得到0001.1.1 00:00:00至现今某个时间点的时间间隔毫秒数. 具体代码实现如下所示: JS: //获取1970.1.1 00:00:00至现在的毫秒数 var milDate = +(new Date); .Net: //获取1970.1.

  • js时间戳转为日期格式的方法

    什么是Unix时间戳(Unix timestamp): Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数.Unix时间戳不仅被使用在Unix系统.类Unix系统中,也在许多其他操作系统中被广泛采用. 目前相当一部分操作系统使用32位二进制数字表示时间.此类系统的Unix时间戳最多可以使用到格林威治时间2038年01月19日

  • js时间戳格式化成日期格式的多种方法

    js需要把时间戳转为为普通格式,一般的情况下可能用不到的, 下面先来看第一种吧 复制代码 代码如下: function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' '); } alert(getLocalTime(1293072805)); 结果是 2010年12月23日 10:53 第二种 复制代码 代码如下: function getLocalTi

  • ASP.NET生成两个日期范围内随机时间的实现方法

    本文实例讲述了ASP.NET生成两个日期范围内随机时间的实现方法.分享给大家供大家参考.具体方法分析如下: 想网上找个生成随机天数的方法找不到,后面只得自己写了,贴给大家方便使用 思路:算两个日期的相差天数,然后在0到相差天数的范围内生成随机数,再用结束时间的天数部分减去这个随机数,代码: 复制代码 代码如下: /// <summary> /// (在两个时间范围内)生成随机日期 /// </summary> /// <param name="startime&qu

  • Asp.net 时间操作基类(支持短日期,长日期,时间差)

    复制代码 代码如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; nam

  • js 格式化时间日期函数小结

    复制代码 代码如下: Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(), //minute "s+" : this.g

  • asp.net(C#)实现功能强大的时间日期处理类完整实例

    本文实例讲述了asp.net(C#)实现功能强大的时间日期处理类.分享给大家供大家参考,具体如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts

  • Js 获取当前日期时间及其它操作实现代码

    myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-????) myDate.getMonth(); //获取当前月份(0-11,0代表1月) myDate.getDate(); //获取当前日(1-31) myDate.getDay(); //获取当前星期X(0-6,0代表星期天) myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数) myDate.getHours();

随机推荐