Date 对于不同的浏览器和国家/地区,使用MVC/Razor处理日期格式的正确或最佳做法是什么?

Date 对于不同的浏览器和国家/地区,使用MVC/Razor处理日期格式的正确或最佳做法是什么?,date,razor,browser,format,Date,Razor,Browser,Format,在我的应用程序(MVC5,bootstrap)中,我有MVC脚手架的默认设置 我希望英国用户的日期显示为dd/mm/yyyy格式。这是日期和注释的数据模型/类声明: [Required, DatabaseGenerated(DatabaseGeneratedOption.Computed)] [DataType(DataType.Date)] [Column(TypeName = "Date")] [DisplayName("Invoice Date")] public DateTime Inv

在我的应用程序(MVC5,bootstrap)中,我有MVC脚手架的默认设置

我希望英国用户的日期显示为dd/mm/yyyy格式。这是日期和注释的数据模型/类声明:

[Required, DatabaseGenerated(DatabaseGeneratedOption.Computed)]
[DataType(DataType.Date)]
[Column(TypeName = "Date")]
[DisplayName("Invoice Date")]
public DateTime InvoiceDate { get; set; } 
这在Internet Explorer中可以正常工作,但在Firefox、Opera或Edge中不行

在后三种情况下编辑时,日期仅在编辑字段中显示dd/mm/yyyy,这可能是因为浏览器提供了自己的编辑控件,并以某种方式依赖于特定的表单。我不确定

我做了一些阅读,并将此注释添加到日期,据说可以解决此问题:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
我尝试了这个方法,它似乎起了作用,但仔细检查后发现,当日期显示为非编辑时,格式错误。浏览器之间也存在一些差异

然后,当我回来在IE上测试它时,日期格式现在是错误的

所以我的问题是,如何在MVC/razor中跨不同浏览器正确处理这个问题?正确或最佳实践方法是什么

当然,处理这一问题并确保用户文化/国家等的正确日期格式的最佳实践方法是什么

先谢谢你

在mvc和EditorFor等视图中使用带绑定的引导,例如:

<div class="form-group">
    @Html.LabelFor(model => model.PaymentDate, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.PaymentDate, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.PaymentDate, "", new { @class = "text-danger" })
    </div>
</div> 

@LabelFor(model=>model.PaymentDate,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.PaymentDate,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.PaymentDate,“,new{@class=“text danger”})
我试过Bootstrap.v3.datetimepicker,但在firefox或opera中不起作用。我显然希望日期选择器具有与其他引导组件相同的外观和感觉


谢谢你的帮助

假设您使用的是
@Html.EditorFor()
@Html.DisplayFor()
(显示您的代码!),那么您正在生成浏览器Html-5日期选择器,这在所有浏览器中都不受支持(IE中根本不受支持,而且仅在最新的FireFox中受支持)。如果您想要一致的UI,那么使用jquery datepicker插件来生成datepickerRefer。另外,在Yes中,代码是
@Html.LabelFor(model=>model.InvoiceDate,htmlAttributes:new{@class=“control label col-md-2”})@Html.EditorFor(model=>model.InvoiceDate,new){htmlAttributes=new{@class=“form control”}}@Html.ValidationMessageFor(model=>model.InvoiceDate,”,new{@class=“text danger”})
---使用引导。是的,一致的用户界面是可取的,但不希望文本框上有任何方形边缘等。试图让引导日期选择器与mvc 5配合使用,但效果不佳:(