.cshtml
<td id="date">
填写日期: @Html.TextBoxFor(p => p.CreateDate, new { @id = "today", @class = "form-control", @style = "border-style:none", @onfocus = "this.blur()" })
</td>
<td id="date">
出差日期:@Html.TextBoxFor(p => p.TravelStart, new { @id = "txtStartDate", @class = "form-control", @onfocus = "this.blur()" })
至 @Html.TextBoxFor(p => p.TravelEnd, new { @id = "txtEndDate", @onfocus = "this.blur()"})
</td>
<script>
$(document).ready(function(){
$("#txtStartDate").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: "yy年m月d日",
onClose: function (selectedDate) {
$("#txtEndDate").datepicker("option", "minDate", selectedDate);
}
});
$("#txtEndDate").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: "yy年m月d日",
onClose: function (selectedDate) {
$("#txtStartDate").datepicker("option", "maxDate", selectedDate);
}
});
$("#today").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: "yy年m月d日",
});
});
jQuery(function ($) {
$.datepicker.regional['zh-CN'] = {
closeText: '关闭',
prevText: '<上月',
nextText: '下月>',
currentText: '今天',
monthNames: ['1月', '2月', '3月', '4月', '5月', '6月',
'7月', '8月', '9月', '10月', '11月', '12月'],
monthNamesShort: ['1', '2', '3', '4', '5', '6',
'7', '8', '9', '10', '11', '12'],
dayNames: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
dayNamesShort: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
dayNamesMin: ['日', '一', '二', '三', '四', '五', '六'],
weekHeader: '周',
dateFormat: 'yy年m月d日',
firstDay: 1,
isRTL: false,
showMonthAfterYear: true,
yearSuffix: '年'
};
$.datepicker.setDefaults($.datepicker.regional['zh-CN']);
});
</script>
document.getElementById("today").value = "@ViewBag.Date";
document.getElementById("txtStartDate").value = "@ViewBag.StartDate";
document.getElementById("txtEndDate").value = "@ViewBag.EndDate";
Controller
ViewBag.Date = DateTime.Now.GetDateTimeFormats()[10];
ViewBag.StartDate= DateTime.Now.GetDateTimeFormats()[10];
ViewBag.EndDate= DateTime.Now.GetDateTimeFormats()[10];
//GetDateTimeFormats()是一个list,里面有多中日期格式
//GetDateTimeFormats()[10]的格式是-年-月-日