js修改时间格式
程序员文章站
2024-01-22 19:27:34
...
getDateTime: function(){
var myDate = new Date();
// document.getElementById("d413").innerHTML ="1233";
// document.getElementById("d413").innerHTML=Date();
var date = this.format(myDate,'yyyy-MM-dd');
$("#d413").attr("value",date);
},
//封装时间格式
format: function(time,format) {
var t = new Date(time);
var tf = function (i) {
return (i < 10 ? '0' : '') + i
};
return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
switch (a) {
case 'yyyy':
return tf(t.getFullYear());
break;
case 'MM':
return tf(t.getMonth() + 1);
break;
case 'mm':
return tf(t.getMinutes());
break;
case 'dd':
return tf(t.getDate());
break;
case 'HH':
return tf(t.getHours());
break;
case 'ss':
return tf(t.getSeconds());
break;
}
})
},