本文给出了在C#中获得当月天数及常用的日期型格式处理方法。
1、如何获得当月有多少天
int m=System.DateTime.DaysInMonth(System.DateTime.Now.Year,System.DateTime.Now.Month);
2、日期型格式处理通用方法
(1)在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;
}
(2)在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" />
(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"];
}
然后在其他地方正常调用就可以了,如果需要修改格式只需要修改webconfig中的,且可以保证整个系统中的所有格式都是一致的。
新闻热点
疑难解答