You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
820 B
35 lines
820 B
7 months ago
|
"use strict";
|
||
|
const timeFormat = (dateTime, fmt = "yyyy-mm-dd") => {
|
||
|
if (!dateTime)
|
||
|
dateTime = Number(new Date());
|
||
|
if (dateTime.toString().length == 10)
|
||
|
dateTime *= 1e3;
|
||
|
const date = new Date(dateTime);
|
||
|
let ret;
|
||
|
const opt = {
|
||
|
"y+": date.getFullYear().toString(),
|
||
|
// 年
|
||
|
"m+": (date.getMonth() + 1).toString(),
|
||
|
// 月
|
||
|
"d+": date.getDate().toString(),
|
||
|
// 日
|
||
|
"h+": date.getHours().toString(),
|
||
|
// 时
|
||
|
"M+": date.getMinutes().toString(),
|
||
|
// 分
|
||
|
"s+": date.getSeconds().toString()
|
||
|
// 秒
|
||
|
};
|
||
|
for (const k in opt) {
|
||
|
ret = new RegExp(`(${k})`).exec(fmt);
|
||
|
if (ret) {
|
||
|
fmt = fmt.replace(
|
||
|
ret[1],
|
||
|
ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0")
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
return fmt;
|
||
|
};
|
||
|
exports.timeFormat = timeFormat;
|