/*
auther:wangt
date:2022-11-04
desc:时间日期相关的方法
*/
//年月日
function getYMD(spt = '/') {
let date = new Date();
let y = date.getFullYear();
let m = date.getMonth() + 1;
let d = date.getDate();
let ymd = [y, fillZreo(m), fillZreo(d)].join(spt);
console.log(ymd);
return ymd;
}
//时分秒
function getHMS(spt = ":") {
let date = new Date();
let h = date.getHours();
let m = date.getMinutes();
let s = date.getSeconds();
let hms = [fillZreo(h), fillZreo(m), fillZreo(s)].join(spt);
console.log(hms);
return hms;
}
//年月日时分秒
function getYMDHMS(spt1, spt2, spt3 = ' ') {
return getYMD(spt1) + spt3 + getHMS(spt2);
}
//补零
function fillZreo(n) {
return (n < 10) ? '0' + n : n;
}
getYMD();
getHMS();
console.log(getYMDHMS('-', '/', ' '));
|