function wcTimeFunc(fmt = 'yyyy-MM-dd hh:mm:ss', time = Date.now(), addition) {
if (fmt == null) {
fmt = 'yyyy-MM-dd hh:mm:ss'
}
if (time == null) {
time = Date.now()
}
if (addition) {
const unitObj = { 'day': 24 * 60 * 60 * 1000, 'hour': 60 * 60 * 1000, 'min': 60 * 1000, 'sec': 1 * 1000 }
let arr = addition.split(' ')
let num = arr[0]
let unit = arr[1]
time = time + num * unitObj[unit]
}
let date = new Date(time);
let o = {
"M+": date.getMonth() + 1,
"d+": date.getDate(),
"h+": date.getHours(),
"m+": date.getMinutes(),
"s+": date.getSeconds(),
t: date.getTime(),
"q+": Math.floor((date.getMonth() + 3) / 3),
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(
RegExp.$1,
(date.getFullYear() + "").substr(4 - RegExp.$1.length)
);
for (let k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
);
return fmt;
}
console.log(wcTimeFunc(null, null, '+8 hour'));
function wcHMS(val) {
let hours = Math.floor(val / 3600);
let mins = Math.floor((val % 3600) / 60);
let seconds = Math.floor((val % 3600) % 60);
function fixZero(v) {
if (v < 10) {
return "0" + v;
}
return v;
}
return `${fixZero(hours)}:${fixZero(mins)}:${fixZero(seconds)}`;
}