您的位置:首页 > 其它

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

2007-05-24 15:15 316 查看
项目要求时间格式要统一,在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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: