datetime-local设置初始值

时间:2022-10-19 04:23:52

//全局变量

var format = "";

//构造符合datetime-local格式的当前日期
function getFormat(){
format = "";
var nTime = new Date();
format += nTime.getFullYear()+"-";
format += (nTime.getMonth()+1)<10?"0"+(nTime.getMonth()+1):(nTime.getMonth()+1);
format += "-";
format += nTime.getDate()<10?"0"+(nTime.getDate()):(nTime.getDate());
format += "T";
format += nTime.getHours()<10?"0"+(nTime.getHours()):(nTime.getHours());
format += ":";
format += nTime.getMinutes()<10?"0"+(nTime.getMinutes()):(nTime.getMinutes());
format += ":00";
}

$(function(){

  document.getElementById("endtime").value = format;//赋初始值

})

p.s:datetime-local必须格式化成"yyyy-MM-ddTHH:mm:ss"的格式