<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