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(j){
switch(j){
case 'yyyy':
return tf(t.getFullYear());
break;
case 'MM':
return tf(t.getMonth() + 1);
break;
case 'dd':
return tf(t.getDate());
break;
case 'HH':
return tf(t.getHours());
break;
case 'mm':
return tf(t.getMinutes());
break;
case 'ss':
return tf(t.getSeconds());
break;
};
});
}
//时间格式传什么显示什么 随意组合
format("2022-08-30","yyyy-MM-dd HH:mm:ss")
format("2022-08-30","MM.dd")
//秒转时分秒
function getTime(time) {
// 转换为式分秒
var h = parseInt(time / 60 / 60 % 24);
var m = parseInt(time / 60 % 60);
var s = parseInt(time % 60);
h = h < 10 ? '0' + h : h;
m = m < 10 ? '0' + m : m;
s = s < 10 ? '0' + s : s;
return h+":"+m+":"+s;
}
getTime(任意秒数);