统一项目时间格式(DateTime.ParseExact使用)

项目要求时间格式要统一,在web.config里能够自定义时间格式,
<add key="DateStringFormat" value="dd/MM/yyyy"/> 这里可以把格式改为任何.net系统里合法的格式,但是本地系统不一定支持。

日历用的是DateSelector_CSharp,从codeproject上下的, 地址是:
http://www.codeproject.com/aspnet/ASPNET_DateSelector.asp

在 该用户控件里获取项目定义的时间格式:

string formatstr = ModuleConfiguration.ModuleConfig.GetConfigValue("DateStringFormat").ToLower();
        
string scriptStr = "javascript:return popUpCalendar(this," + getClientID() + @", '"+formatstr+@"', '__doPostBack(\'" + getClientID() + @"\')')";
        
//Response.Write(scriptStr);
        imgCalendar.Attributes.Add("onclick", scriptStr);
注意:这里的时间格式放在js里面就要都改为小写形式,.net里面是dd/MM/yyyy,js里面就是dd/mm/yyyy。

这样textox里面是项目定义的时间格式了,
保存的时候就要用到DateTime.ParseExact

DateTime.ParseExact(AAA.CalendarDate,ModuleConfiguration.ModuleConfig.GetConfigValue("DateStringFormat"),DateTimeFormatInfo.InvariantInfo);
DateTimeFormatInfo.InvariantInfo为忽略本地系统时间格式。
ModuleConfiguration.ModuleConfig.GetConfigValue("DateStringFormat")为项目自定义时间格式。
AAA.CalendarDate为选择的时间。

在更新状态下:
从数据库里读出的时间也要用项目自定义格式格式化。

((DateTime)dt.Rows[0]["C_Date"]).ToString(ModuleConfiguration.ModuleConfig.GetConfigValue("DateStringFormat"), DateTimeFormatInfo.InvariantInfo);


原文地址:https://www.cnblogs.com/panzhilei/p/758595.html