给html5日期控件type=“date“赋值 | js 日期格式化 yyyy-MM-dd

<input type="date" value=""> 的value值,只能是"yyyy-MM-dd",这样的,例如:“2020-9-27” 这样

代码如下:

<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>

<input type="date" class="defaultTxt" id="txtEnteringDate_edit" />

<script>
     $.ajax({
                async: false,
                type: "get",
                url: "/Recommend/GetRecommendById",
                data: {
                    id: id
                },
                dataType: "json",
                success: function (json) {
        $("#txtEnteringDate_edit").val(dayFormat(json.EnteringDate.replace("T00:00:00","")));         
                },
            });

        //日期格式化 
        function dayFormat(dt) {
            var date = new Date(dt);
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var day = date.getDate();
            if (month < 10) {
                month = "0" + month;
            }
            if (day < 10) {
                day = "0" + day;
            }
            var nowDate = year + "-" + month + "-" + day;
            return nowDate;
        }
</script>

初始化,日期控件为今天:

日期:<input type="date" id="dtDate" />

<script>
      $(function () {
            DateNow();
        });
        function DateNow() {
             var now = new Date();
            var nowYear = now.getFullYear(); //年
            var nowMonth = now.getMonth() + 1 < 10 ? "0" + (now.getMonth() + 1) : now.getMonth(); //月
            var nowDay = now.getDate() < 10 ? "0" + now.getDate() : now.getDate(); //日期
            var nowDate = nowYear + "-" + nowMonth + "-" + nowDay;
            $("#dtDate").val(nowDate);
        }

</script>

 

其他:https://blog.csdn.net/u014063717/article/details/50914466

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值