.NET datepicker格式给出了"MM-dd-yyyy"而不是"MM/dd/yyyy" [英] .NET datepicker format gives "MM-dd-yyyy" instead of "MM/dd/yyyy"
问题描述
我已经在我的MVC中创建了一个日期选择器,该日期选择器工作正常,但是当我选择一个日期时,会得到格式"MM/dd/yyyy".保存后,它会变回"MM-dd-yyyy".如果我尝试使用这种格式保存,日期选择器会说这不是有效的日期,因为它已将分隔符从"/"更改为-". 因此,我希望保留这样的格式:"MM/dd/yyyy".
I have created a datepicker in my MVC that works fine but when i pick a date I get the format "MM/dd/yyyy". When I save that, it changes back to "MM-dd-yyyy". If i try to save with this format the datepicker says that it is not a valid date because it has changed the seperater from "/" to "-". So I wish to maintain a format like this "MM/dd/yyyy".
这是我实现日期选择器的字段的代码:
Here is my code for the field where the datepicker is implemented:
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.TimeFrom, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.TimeFrom)*@
@Html.TextBoxFor(model => model.TimeFrom, "{0:MM/dd/yyyy}", new { @class = "datepicker form-control"})
@Html.ValidationMessageFor(model => model.TimeFrom)
</div>
</div>
这是我的约会选择者:
<script type="text/javascript">
$(document).ready(function () {
$('.datepicker').each(function () {
$(this).datepicker({
changeMonth: true,
changeYear: true
});
});
});
</script>
推荐答案
将格式添加到您的datepicker
选项:
Add the format to your datepicker
options:
<script type="text/javascript">
$(document).ready(function () {
$('.datepicker').each(function () {
$(this).datepicker({
changeMonth: true,
changeYear: true,
dateFormat: "mm/dd/yy"
});
});
});
</script>
datepicker
格式与ASP.NET格式略有不同. mm
等同于MM
,yy
等同于yyyy
.
The datepicker
format is slightly different from the ASP.NET format. mm
is the equivalent of MM
, and yy
is the equivalent of yyyy
.
这篇关于.NET datepicker格式给出了"MM-dd-yyyy"而不是"MM/dd/yyyy"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!