常見日期方法薈萃

一.如何獲得當月有多少天
int m=System.DateTime.DaysInMonth(System.DateTime.Now.Year,System.DateTime.Now.Month);
二.日期型格式處理通用方法
1.在webconfig中配置如下
<add key="ShortDatePattern" value="MM-dd-yyyy" />
<add key="LongDatePattern" value="dddd-MMMM dd-yyyy" />
<add key="ShortTimePattern" value="hh:mm tt" />
<add key="LongTimePattern" value="hh:mm tt" />
2.在global.asax中
protected void Application_BeginRequest(Object sender, EventArgs e)
{
Thread currentThread 
= Thread.CurrentThread;
CultureInfo cul 
= currentThread.CurrentCulture.Clone() as CultureInfo;
cul.DateTimeFormat.ShortDatePattern
= BLLFacade.Common.GetShortDatePattern();
cul.DateTimeFormat.LongDatePattern
= BLLFacade.Common.GetLongDatePattern();
cul.DateTimeFormat.ShortTimePattern
= BLLFacade.Common.GetShortTimePattern();
cul.DateTimeFormat.LongTimePattern
= BLLFacade.Common.GetLongTimePattern();
currentThread.CurrentCulture 
= cul;
}
3.在業務邏輯層中
public static string GetShortDatePattern()
{
return System.Configuration.ConfigurationSettings.AppSettings["ShortDatePattern"];
}


public static string GetLongDatePattern()
{
return System.Configuration.ConfigurationSettings.AppSettings["LongDatePattern"];
}


public static string GetShortTimePattern()
{
return System.Configuration.ConfigurationSettings.AppSettings["ShortTimePattern"];
}


public static string GetLongTimePattern()
{
return System.Configuration.ConfigurationSettings.AppSettings["LongTimePattern"];
}
4.然後在其他地方正常調用就可以了,如果需要修改格式只需要修改webconfig中的,且可以保證整個系統中的所有格式都是一致的
三.在asp.net中怎麼樣計算兩個日期相差的年、月份、日期、小時、分鐘 、妙等
在asp.net中怎麼樣計算兩個日期相差的年、月份、日期、小時、分鐘 、妙等
四.獲取某月的實際工作日(即不包括週六日)
//調用
//int days =getDays(System.DateTime.Now));
private int getDays(System.DateTime date1)
{
    
int m=System.DateTime.DaysInMonth(date1.Year,date1.Month);
    
int mm=0;
    
for(int i=1;i<=m;i++)
    
{
        System.DateTime date
=Convert.ToDateTime(date1.Year+"-"+date1.Month+"-"+i);
        
switch (date.DayOfWeek)
        
{
            
case System.DayOfWeek.Monday:
            
case System.DayOfWeek.Thursday:
            
case System.DayOfWeek.Tuesday:
            
case System.DayOfWeek.Wednesday:
            
case System.DayOfWeek.Friday:
                mm
=mm+1;
                
break;

        }
                
    }

    
return mm;
}
五.獲得任意兩日期之間的有效工作日(不包括週六日)
獲得任意兩日期之間的有效工作日(不包括週六日)
六.格式輸出
格式輸出
七.獲得本週的週六和週日
ConvertDateToWeek    

//調用
DateTime firstdate=System.DateTime.Now;    
DateTime lastdate
=System.DateTime.Now;
ConvertDateToWeek(date,
out firstdate,out lastdate);
發佈了159 篇原創文章 · 獲贊 3 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章