给input的value赋予默认值为当天日期

本文详细介绍如何在ASP.NET MVC项目中使用jQuery DatePicker插件来选择日期,包括出差日期的选择和当前日期的显示。通过设置区域选项,DatePicker能正确显示中文日期格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.cshtml

<td id="date">
填写日期: &nbsp;&nbsp;@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]的格式是-年-月-日
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值