function format(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) {
console.log('a', 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;
}
});
}
let date = format('2022-07-07 00:09:41', 'yyyy-MM-dd');
console.log('date', date);
js处理时间日期函数
最新推荐文章于 2024-04-18 15:35:03 发布