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

时间: 2024-10-03 22:34:40

asp.net下日期和时间处理的类库_实用技巧的相关文章

ASP.NET页面请求超时时间设置多种方法_实用技巧

ASP.NET 页面请求超时时间(页面后台程序执行时间)默认值为110秒(在 .NET Framework 1.0 版和 1.1 版中,默认值为 90 秒) 即: Server.ScriptTimeout = 110(HttpServerUtility.ScriptTimeout = 110) System.Web.Configuration.HttpRuntimeSection().ExecutionTimeout.ToString() =00:01:50(110 秒) 方法一:设置 Serv

asp.net下大文件上传知识整理_实用技巧

最近做在做ePartner项目,涉及到文件上传的问题. 以前也做过文件上传,但都是些小文件,不超过2M. 这次要求上传100M以上的东西. 没办法找来资料研究了一下.基于WEB的文件上传可以使用FTP和HTTP两种协议,用FTP的话虽然传输稳定,但安全性是个严重的问题,而且FTP服务器读用户库获取权限,这样对于用户使用来说还是不太方便. 剩下只有HTTP.在HTTP中有3种方式,PUT.WEBDAV.RFC1867,前2种方法不适合大文件上传,目前我们使用的web上传都是基于RFC1867标准的

asp.net下cookies的丢失和中文乱码_实用技巧

我昨天晚上在做相册的时候突然又发现,iis下cookies里的中文又出现问题了,显示乱码. 费了好一段时间,才解决: //写入cookies时 string t =HttpUtility.UrlEncode(要写入到cookies的中文值); HttpCookie c = new HttpCookie("user_realname", t); Response.Cookies.Add(c); //读取cookies时 t=获取的中文cookies值; t = HttpUtility.U

asp.net下Repeater使用 AspNetPager分页控件_实用技巧

一.AspNetPager分页控件 分页是Web应用程序中最常用到的功能之一,在ASP.NET中,虽然自带了一个可以分页的DataGrid(asp.net 1.1)和GridView(asp.net 2.0)控件,但其分页功能并不尽如人意,如可定制性差.无法通过Url实现分页功能等,而且有时候我们需要对DataList和Repeater甚至自定义数据绑定控件进行分页,手工编写分页代码不但技术难度大.任务繁琐而且代码重用率极低,因此分页已成为许多ASP.NET程序员最头疼的问题之一. AspNet

ASP.NET下对cookies的操作实现代码_实用技巧

复制代码 代码如下: public class BsCookie { //操作的cookie private HttpCookie _theCookie; //对应的cookie的名称 private string _cookieName; private bool _httpOnly = true; /// <summary> /// 是否只允许在服务器端访问,默认只允许在服务端访问 /// </summary> public bool HttpOnly { get { retu

asp.net下中文验证码,免费开源代码_实用技巧

using System; using System.Data; using System.Configuration; using System.Collections; 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.Ht

asp.net下获取浏览器类型的实现代码_实用技巧

相关代码: 复制代码 代码如下: if (Page.Request.Browser.EcmaScriptVersion.Major > 0 && Page.Request.Browser.W3CDomVersion.Major > 0)//判断是否支持脚本 { Response.Write("浏览器名称与版本号:" + Page.Request.Browser.Type + "<br />"); Response.Write(

asp.net下使用DIME协议上传文件_实用技巧

在某些Web Service的应用场景下,例如公文的传送,在Web Service返回结果的同时将word文档及其它附件返回,这时候可以使用DIME协议来进行文件的传输.使用它来传输不需要经过SOAP消息的序列化/反序列化,有很高的效率.当然这里要用到Web Services Enhancements (WSE) ,目前的最新版本为3.0.本文中所使用的版本为2.0sp2,有趣的是WSE的各个版本中的命令空间都有很大的变化.这一点的确有点让人苦恼!在安装WSE时推荐将Visual Studio 

asp.net下用DataSet生成XML的问题_实用技巧

格式限定符中往往有一些字母代表特点的意思 比如,在DateTime.ToString中,y代码年,M代表月,等等 那么如果ToString的结果我本身就需要这些字符又怎么办呢? 此时就需要使用转义字符,而且这个转义标记就是我们熟悉的"\" DateTime now = DateTime.Now; now.ToString("yyyy,MM,dd HH:mm:ffff");//2006,07,06 14:51:3425 now.ToString(@"\yea