常见日期方法荟萃

一.如何获得当月有多少天
None.gifint m=System.DateTime.DaysInMonth(System.DateTime.Now.Year,System.DateTime.Now.Month);
二.日期型格式处理通用方法
1.在webconfig中配置如下
None.gif<add key="ShortDatePattern" value="MM-dd-yyyy" />
None.gif
<add key="LongDatePattern" value="dddd-MMMM dd-yyyy" />
None.gif
<add key="ShortTimePattern" value="hh:mm tt" />
None.gif
<add key="LongTimePattern" value="hh:mm tt" />
2.在global.asax中
None.gifprotected void Application_BeginRequest(Object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifThread currentThread 
= Thread.CurrentThread;
InBlock.gifCultureInfo cul 
= currentThread.CurrentCulture.Clone() as CultureInfo;
InBlock.gifcul.DateTimeFormat.ShortDatePattern
= BLLFacade.Common.GetShortDatePattern();
InBlock.gifcul.DateTimeFormat.LongDatePattern
= BLLFacade.Common.GetLongDatePattern();
InBlock.gifcul.DateTimeFormat.ShortTimePattern
= BLLFacade.Common.GetShortTimePattern();
InBlock.gifcul.DateTimeFormat.LongTimePattern
= BLLFacade.Common.GetLongTimePattern();
InBlock.gifcurrentThread.CurrentCulture 
= cul;
ExpandedBlockEnd.gif}
3.在业务逻辑层中
None.gifpublic static string GetShortDatePattern()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
return System.Configuration.ConfigurationSettings.AppSettings["ShortDatePattern"];
ExpandedBlockEnd.gif}

None.gif
None.gif
public static string GetLongDatePattern()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
return System.Configuration.ConfigurationSettings.AppSettings["LongDatePattern"];
ExpandedBlockEnd.gif}

None.gif
None.gif
public static string GetShortTimePattern()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
return System.Configuration.ConfigurationSettings.AppSettings["ShortTimePattern"];
ExpandedBlockEnd.gif}

None.gif
None.gif
public static string GetLongTimePattern()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
return System.Configuration.ConfigurationSettings.AppSettings["LongTimePattern"];
ExpandedBlockEnd.gif}
4.然后在其他地方正常调用就可以了,如果需要修改格式只需要修改webconfig中的,且可以保证整个系统中的所有格式都是一致的
三.在asp.net中怎么样计算两个日期相差的年、月份、日期、小时、分钟 、妙等
ContractedBlock.gifExpandedBlockStart.gif在asp.net中怎么样计算两个日期相差的年、月份、日期、小时、分钟 、妙等#region 在asp.net中怎么样计算两个日期相差的年、月份、日期、小时、分钟 、妙等
InBlock.gif
//        调用
InBlock.gif
//        DateTime a=Convert.ToDateTime("2005-09-03 20:15");
InBlock.gif
//        DateTime b=Convert.ToDateTime("2005-09-04 09:09 ");
InBlock.gif
//        double d=Bll.Common.DateDiff(Bll.Common.EnumDateCompare.day,a,b);
InBlock.gif
//        Response.Write(d.ToString("f0"));//四舍五入
InBlock.gif
        public enum EnumDateCompare
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            year    
=1,
InBlock.gif            month    
=2,
InBlock.gif            day        
=3,
InBlock.gif            hour    
=4,
InBlock.gif            minute    
=5,
InBlock.gif            second    
=6
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static double DateDiff(EnumDateCompare howtocompare, System.DateTime startDate, System.DateTime endDate) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
double diff=0
InBlock.gif            System.TimeSpan TS 
= new System.TimeSpan(endDate.Ticks-startDate.Ticks); 
InBlock.gif
InBlock.gif            
switch (howtocompare) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
case EnumDateCompare.year: 
InBlock.gif                    diff 
= Convert.ToDouble(TS.TotalDays/365); 
InBlock.gif                    
break
InBlock.gif                
case EnumDateCompare.month: 
InBlock.gif                    diff 
= Convert.ToDouble((TS.TotalDays/365)*12); 
InBlock.gif                    
break
InBlock.gif                
case EnumDateCompare.day:
InBlock.gif                    diff 
= Convert.ToDouble(TS.TotalDays); 
InBlock.gif                    
break
InBlock.gif                
case EnumDateCompare.hour: 
InBlock.gif                    diff 
= Convert.ToDouble(TS.TotalHours); 
InBlock.gif                    
break
InBlock.gif                
case EnumDateCompare.minute: 
InBlock.gif                    diff 
= Convert.ToDouble(TS.TotalMinutes); 
InBlock.gif                    
break
InBlock.gif                
case EnumDateCompare.second: 
InBlock.gif                    diff 
= Convert.ToDouble(TS.TotalSeconds); 
InBlock.gif                    
break
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return diff;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif        
#endregion
四.获取某月的实际工作日(即不包括周六日)
None.gif//调用
None.gif
//int days =getDays(System.DateTime.Now));
None.gif
private int getDays(System.DateTime date1)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
int m=System.DateTime.DaysInMonth(date1.Year,date1.Month);
InBlock.gif    
int mm=0;
InBlock.gif    
for(int i=1;i<=m;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.DateTime date
=Convert.ToDateTime(date1.Year+"-"+date1.Month+"-"+i);
InBlock.gif        
switch (date.DayOfWeek)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
case System.DayOfWeek.Monday:
InBlock.gif            
case System.DayOfWeek.Thursday:
InBlock.gif            
case System.DayOfWeek.Tuesday:
InBlock.gif            
case System.DayOfWeek.Wednesday:
InBlock.gif            
case System.DayOfWeek.Friday:
InBlock.gif                mm
=mm+1;
InBlock.gif                
break;
InBlock.gif
ExpandedSubBlockEnd.gif        }
                
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return mm;
ExpandedBlockEnd.gif}
五.获得任意两日期之间的有效工作日(不包括周六日)
ContractedBlock.gifExpandedBlockStart.gif获得任意两日期之间的有效工作日(不包括周六日)#region 获得任意两日期之间的有效工作日(不包括周六日)
InBlock.gif        
//调用
InBlock.gif        
//DateTime date1=Convert.ToDateTime("2005-10-20");
InBlock.gif        
//DateTime date2=Convert.ToDateTime("2005-11-01");
InBlock.gif        
//int days =getDays(date1,date2);
InBlock.gif
        private int getDays(System.DateTime date1,System.DateTime date2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string m=DateDiff(EnumDateCompare.day,date1,date2).ToString("f0");
InBlock.gif            
InBlock.gif            
int mm=0;
InBlock.gif            
for(int i=0;i<=Convert.ToInt32(m);i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.DateTime date
=Convert.ToDateTime(date1.AddDays(i));
InBlock.gif                
switch (date.DayOfWeek)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
case System.DayOfWeek.Monday:
InBlock.gif                    
case System.DayOfWeek.Thursday:
InBlock.gif                    
case System.DayOfWeek.Tuesday:
InBlock.gif                    
case System.DayOfWeek.Wednesday:
InBlock.gif                    
case System.DayOfWeek.Friday:
InBlock.gif                        mm
=mm+1;
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                }
                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return mm;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion
六.格式输出
ContractedBlock.gifExpandedBlockStart.gif格式输出#region 格式输出
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Globalization.DateTimeFormatInfo myDTFI 
= new System.Globalization.CultureInfo( "en-US"false ).DateTimeFormat;//中国用zh-cn
InBlock.gif
            DateTime myDT =System.DateTime.Now;
InBlock.gif
InBlock.gif            Response.Write(myDT.ToString(
"f",myDTFI));
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//**//**//*
InBlock.gif            This code produces the following output.
InBlock.gif
InBlock.gif            FORMAT  en-US EXAMPLE
InBlock.gif            CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY
InBlock.gif
InBlock.gif              d     1/3/2002
InBlock.gif                    M/d/yyyy (ShortDatePattern)
InBlock.gif
InBlock.gif              D     Thursday, January 03, 2002
InBlock.gif                    dddd, MMMM dd, yyyy (LongDatePattern)
InBlock.gif
InBlock.gif              f     Thursday, January 03, 2002 12:00 AM
InBlock.gif
InBlock.gif              F     Thursday, January 03, 2002 12:00:00 AM
InBlock.gif                    dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)
InBlock.gif
InBlock.gif              g     1/3/2002 12:00 AM
InBlock.gif
InBlock.gif              G     1/3/2002 12:00:00 AM
InBlock.gif
InBlock.gif              m     January 03
InBlock.gif                    MMMM dd (MonthDayPattern)
InBlock.gif
InBlock.gif              M     January 03
InBlock.gif                    MMMM dd (MonthDayPattern)
InBlock.gif
InBlock.gif              r     Thu, 03 Jan 2002 00:00:00 GMT
InBlock.gif                    ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
InBlock.gif
InBlock.gif              R     Thu, 03 Jan 2002 00:00:00 GMT
InBlock.gif                    ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
InBlock.gif
InBlock.gif              s     2002-01-03T00:00:00
InBlock.gif                    yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)
InBlock.gif
InBlock.gif              t     12:00 AM
InBlock.gif                    h:mm tt (ShortTimePattern)
InBlock.gif
InBlock.gif              T     12:00:00 AM
InBlock.gif                    h:mm:ss tt (LongTimePattern)
InBlock.gif
InBlock.gif              u     2002-01-03 00:00:00Z
InBlock.gif                    yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)
InBlock.gif
InBlock.gif              U     Thursday, January 03, 2002 8:00:00 AM
InBlock.gif
InBlock.gif              y     January, 2002
InBlock.gif                    MMMM, yyyy (YearMonthPattern)
InBlock.gif
InBlock.gif              Y     January, 2002
InBlock.gif                    MMMM, yyyy (YearMonthPattern)
ExpandedSubBlockEnd.gif            
*/

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }
        
ExpandedBlockEnd.gif        
#endregion
七.获得本周的周六和周日
ContractedBlock.gifExpandedBlockStart.gifConvertDateToWeek#region ConvertDateToWeek
InBlock.gif        
public static void ConvertDateToWeek(DateTime date,out DateTime firstdate,out DateTime lastdate)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            DateTime first
=System.DateTime.Now;
InBlock.gif            DateTime last
=System.DateTime.Now;
InBlock.gif            
switch (date.DayOfWeek)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{                
InBlock.gif                
case System.DayOfWeek.Monday:
InBlock.gif                    first
=date.AddDays(-1);
InBlock.gif                    last
=date.AddDays(5);
InBlock.gif                    
break;
InBlock.gif                
case System.DayOfWeek.Tuesday:
InBlock.gif                    first
=date.AddDays(-2);
InBlock.gif                    last
=date.AddDays(4);
InBlock.gif                    
break;
InBlock.gif                
case System.DayOfWeek.Wednesday:
InBlock.gif                    first
=date.AddDays(-3);
InBlock.gif                    last
=date.AddDays(3);
InBlock.gif                    
break;
InBlock.gif                
case System.DayOfWeek.Thursday:
InBlock.gif                    first
=date.AddDays(-4);
InBlock.gif                    last
=date.AddDays(2);
InBlock.gif                    
break;
InBlock.gif                
case System.DayOfWeek.Friday:
InBlock.gif                    first
=date.AddDays(-5);
InBlock.gif                    last
=date.AddDays(1);
InBlock.gif                    
break;
InBlock.gif                
case System.DayOfWeek.Saturday:
InBlock.gif                    first
=date.AddDays(-6);
InBlock.gif                    last
=date;
InBlock.gif                    
break;
InBlock.gif                
case System.DayOfWeek.Sunday:
InBlock.gif                    first
=date;
InBlock.gif                    last
=date.AddDays(6);
InBlock.gif                    
break;                
ExpandedSubBlockEnd.gif            }

InBlock.gif            firstdate
=first;
InBlock.gif            lastdate
=last;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion
    
None.gif
None.gif
//调用
None.gif
DateTime firstdate=System.DateTime.Now;    
None.gifDateTime lastdate
=System.DateTime.Now;
None.gifConvertDateToWeek(date,
out firstdate,out lastdate);
八获得当前日期是该年度的第几周
None.gifDateTime dt = Convert.ToDateTime("2006-05-01");
None.gif
int weeks = dt.DayOfYear / 7 + 1;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/408165.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

socket的阻塞模式和非阻塞模式(send和recv函数在阻塞和非阻塞模式下的表现)

socket的阻塞模式和非阻塞模式 无论是Windows还是Linux,默认创建socket都是阻塞模式的 在Linux中,可以再创建socket是直接将它设置为非阻塞模式 int socket (int __domain, int __type, int __protocol)将__type增加SOCK_NOBLOCK 不仅如此,在Linux上直接利用accept函数返回…

connect函数在阻塞和非阻塞模式下的行为

connect函数在阻塞和非阻塞模式下的行为 当socket使用阻塞模式时,connect函数会阻塞到有明确结果才会返回,如果网络环境较差,可能要等一会,影响体验, 为了解决这个问题,我们使用异步connect技术 创建socket,将socket设置为非阻塞模式 调用connect函数,此时无论connect函数是…

获取socket对应的接收缓冲区中的可读数据量

获取socket对应的接收缓冲区中的可读数据量 本文介绍如何获取当前socket对应的接收缓冲区的可读数据量 在Linux上可以使用ioctl函数 #include <sys/ioctl.h>int ioctl (int __fd, unsigned long int __request, ...)来看一个例子: #include <sys/types.h> #in…

Linux epoll的用法

Linux epoll的用法 epollfd_create函数 #include <sys/epoll.h>int epoll_create (int __size)参数含义__size此参数从Linux 2.6.8后就不再使用了,但必须设置成大于零的值 返回值含义>0可用的epollfd-1调用失败 epollfd_ctl函数 有了epollfd,我们需要将要检测事件…

windows网络编程

windows网络编程 TCP编程 服务端 这里我们有几点需要注意: 使用WSAStartup初始化网络库,即将与socket函数相关dll文件加载到进程地址空间中退出时,使用WSACleanup()卸载相关dll文件与Linux使用close函数关闭socket不同,windows需要使用closesocket函数关闭socket WSAStart…

TCP服务器epoll的多种实现

TCP服务器epoll的多种实现 对于网络IO会涉及到两个系统对象 用户空间中进程或者线程操作系统内核 比如发生read操作时就会经历两个阶段 等待数据就绪将数据从内核缓冲区拷贝到用户缓冲区 由于各个阶段多有不同的情况,一组合么就产生了多种网络 IO 模型 阻塞IO 在Linux中…

侯捷面向对象高级编程(二)

侯捷面向对象高级编程(二) 转换函数 转换函数没有返回值,返回值就是double即函数名,不需要自己写因为转换函数一般不会改变其中内容,所以要加const限定 两条路都可以走,就回产生歧义,报错 explict禁止自动转换,于是4无法转转换为Fraction pointer-like cliasses ->作用之后…

拒绝了对对象 'sp_sdidebug'(数据库 'master',所有者 'dbo')的 EXECUTE 权限

在.net中调用时出现“拒绝了对对象 sp_sdidebug&#xff08;数据库 master&#xff0c;所有者 dbo&#xff09;的 EXECUTE 权限”的错误的解决办法。该问题是我在用指定的URL启动项目后&#xff0c;再“附加进程”后运行程序时出现的。该问题主要是.net2005的调试机制引起的&am…

ASP.NET MVC 音乐商店 - 6. 使用 DataAnnotations 进行模型验证

在前面的创建专辑与编辑专辑的表单中存在一个问题&#xff1a;我们没有进行任何验证。字段的内容可以不输入&#xff0c;或者在价格的字段中输入一些字符&#xff0c;在执行程序的时候&#xff0c;这些错误会导致数据库保存过程中出现错误&#xff0c;我们将会看到来自数据库的…