Global.asax.cs网站自定义日期格式
本文关键字:日期 格式 自定义 网站 asax cs Global | 更新日期: 2023-09-27 18:24:49
我正在开发一个ASP.NET C#Webforms网站,使用MySQL作为数据库,以其标准日期格式提供日期。
如何将自定义日期格式添加到Global.asax.cs文件中,如下所示(非工作代码):
using System.Globalization;
using System.Threading;
protected void Application_BeginRequest(Object sender, EventArgs e)
{
CultureInfo newCulture = (CultureInfo) System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
//Will make all dates of the format similar to 3/14/13 12:9 AM/PM
newCulture.DateTimeFormat = "M/d/yy h:m tt";
Thread.CurrentThread.CurrentCulture = newCulture;
}
感谢您的意见。
DateTimeFormat
属性实际上是一个类型为DateTimeFormatInfo
的对象,具有许多不同的属性。试试这个:
CultureInfo newCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
newCulture.DateTimeFormat.ShortDatePattern = "M/d/yy";
newCulture.DateTimeFormat.ShortTimePattern = "h:m tt";
newCulture.DateTimeFormat.LongDatePattern = "M/d/yy";
newCulture.DateTimeFormat.LongTimePattern = "h:m tt";
Thread.CurrentThread.CurrentCulture = newCulture;
现在,如果你只是在你的ASP.Net代码中做这样的事情:
<%= DateTime.Now %>
它应该学习你的文化中的格式。当然,它很容易被覆盖:
<%= DateTime.Now.ToString("ss:mm:HH dd/MM/yyyy") %> // backwards!
你无法阻止这种情况的发生。您所能做的就是更改默认值。
短期和长期模式可能是你唯一需要改变的模式。默认(常规)模式是由这些模式构建的。