加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将日期格式解析为特定文化

发布时间:2020-12-16 01:44:58 所属栏目:百科 来源:网络整理
导读:我住在南非文化en-ZA,我们的日期格式输入格式为dd / mm / yyyy 我有一个接受模型的视图: public class UserInfoModel{ public DateTime DateOfBirth{get;set;} // some other properties here} 当用户输入日期即:04/15/1981时,我在邮寄方法中的日期时间是1
我住在南非文化en-ZA,我们的日期格式输入格式为dd / mm / yyyy

我有一个接受模型的视图:

public class UserInfoModel
{
    public DateTime DateOfBirth{get;set;}
    // some other properties here
}

当用户输入日期即:04/15/1981时,我在邮寄方法中的日期时间是1981年4月15日,但是,当插入以下日期时,在返回的模型中,DateOfBirth属性为null

有没有办法可以改变全局解析日期的方式(在我的应用程序中)

我在web.config中添加了以下内容:

<system.web>
  <globalization culture="en-ZA" uiCulture="en-ZA"/>
</system.web>

但它似乎没有什么区别.

解决方法

尝试将此添加到您的GlobalAsax.cs(位于您的App_code目录中)

protected void Application_BeginRequest(object sender,EventArgs e)
{
    CultureInfo cInfo = new CultureInfo("en-ZA");
    cInfo.DateTimeFormat.ShortDatePattern = "dd-MM-yyyy";
    cInfo.DateTimeFormat.DateSeparator = "/";
    Thread.CurrentThread.CurrentCulture = cInfo;
    Thread.CurrentThread.CurrentUICulture = cInfo;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读