[英]HTML5 date of format dd/MMM/YYYY
I'm having trouble using date of format dd/MMM/YYYY on MVC5 asp.net project. 我在MVC5 asp.net项目上使用dd / MMM / YYYY格式的日期时遇到问题。 Mostly on Chrome, as it seem to only accept dates of format yyyy/mm/dd. 主要是在Chrome上,因为它似乎只接受yyyy / mm / dd格式的日期。
To normalize behavior across browsers, I'm using a jquery datetimepicker component. 为了规范浏览器的行为,我使用了一个jquery datetimepicker组件。
I have tried many things, but Chrome is still saying that the date is not valid. 我已经尝试了很多方法,但是Chrome仍在说该日期无效。 Even after define the input as text instead of date. 即使在将输入定义为文本而不是日期之后。
Also even if I turn off validation for that particular component (data-val="false"), Chrome still insist on marking the date as invalid. 同样,即使我关闭了对特定组件的验证(data-val =“ false”),Chrome仍然坚持将日期标记为无效。
Model: 模型:
public class order
{
[Key]
public int orderID { get; set; }
[Required]
public string comments { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:dd/MMM/yyyy}", ApplyFormatInEditMode = false)]
[Display(Name = "Date of Shipping")]
public DateTime date { get; set; }
}
View date input: 查看日期输入:
<div class="form-group">
@Html.LabelFor(model => model.date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.date, new { htmlAttributes = new { @class = "form-control datetimepicker" } })
@Html.ValidationMessageFor(model => model.date, "", new { @class = "text-danger" })
</div>
</div>
Imports: 进口:
<script src="https://code.jquery.com/jquery-3.2.1.js"/script>
<link rel="stylesheet" type="text/css" href="~/Content/jquery.datetimepicker.min.css" />
<script src="~/Scripts/jquery.datetimepicker.full.min.js"></script>
Javascript: Javascript:
<script type="text/javascript">
jQuery.datetimepicker.setLocale('es');
jQuery('#date').datetimepicker({
format: 'd/M/Y',
});
</script>
I was finally able to fix the issue: 我终于能够解决此问题:
-Fisrt you need to add moment.js to your project, it can be added using NutGet manager, then reference on your pages: -首先,您需要将moment.js添加到您的项目中,可以使用NutGet管理器添加它,然后在页面上进行引用:
@Scripts.Render("~/Scripts/moment-with-locales.min.js")
Very important, if working with dates on non English format, you need to import moment-with-locales as if not, even after change locale it will stay as 'en' 非常重要,如果使用非英语格式的日期,则需要导入与区域设置相同的时刻,即使不是,即使更改区域设置后,它也将保持为“ en”
-Then add following java script code: -然后添加以下Java脚本代码:
$.datetimepicker.setDateFormatter({
parseDate: function (date, format) {
var d = moment(date, format);
return d.isValid() ? d.toDate() : false;
},
formatDate: function (date, format) {
return moment(date).format(format);
}
});
$.validator.methods.date = function (value, element) {
return this.optional(element) || moment(value, "DD/MMM/YYYY", 'es').isValid();
}
-Also note that datetimepicker initialization changes as follows: -还请注意,datetimepicker初始化的更改如下:
jQuery('#date').datetimepicker({
format: 'DD/MMM/YYYY HH:mm',
formatTime: 'HH:mm',
formatDate: 'DD/MMM/YYYY'
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.