【问题标题】:Datepicker datetime format issue. behaviour changes if change the local system date formatDatepicker 日期时间格式问题。如果更改本地系统日期格式,行为会发生变化
【发布时间】:2012-09-03 19:45:01
【问题描述】:

我是 MVC3 的新手,正在为我的公司做一项评估工作。今天,我被 MVC3 中的日期格式卡住了。

我有一个具有以下方法的模型类

[DataType(DataType.Date)]
[Display(Name = "Date Of Birth")]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)] 
public DateTime? DOB { get; set; }

[DataType(DataType.Date)]
[Display(Name = "Date Of Joining")]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)] 
public DateTime? DOJ { get; set; }

在我看来,我调用了一个 jquery 内置函数 Datepicker

<script language="javascript" type="text/javascript">
    $(function () {
        $("#DOB").datepicker({
                                 changeMonth: true,
                                 changeYear: true,
                                 yearRange: "1942:2099",
                                 dateFormat: 'dd/mm/yy' 
                            });
        $("#DOJ").datepicker({
                                 changeMonth: true,
                                 changeYear: true,
                                 yearRange: "2010:2099",
                                 dateFormat: 'dd/mm/yy' 
                            });
    });
</script>

如果我的本地系统日期格式为“dd-mm-yyyy”格式,则此代码工作正常,但如果我在本地系统中更改任何其他格式的日期格式,则开始抛出错误。

这里我的问题是,我怎样才能使它成为通用日期时间格式,这样它就不会在部署后抛出错误?

【问题讨论】:

    标签: jquery asp.net-mvc-3


    【解决方案1】:

    听起来问题与文化设置有关。我在日期格式方面遇到了类似的问题,我通过在 web.config 的 &lt;system.web&gt; 部分添加这些行来解决它

    <globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB"/>
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-20
      相关资源
      最近更新 更多