时间格式 : ‘"YYYY-MM-DD"’,‘yyy-MM-dd hh:mm:ss’ , "YYYY/MM/DD hh:mm:ss"
class TimeUtil {
constructor(options) {
this.options = options ? options : {};
}
// 年月日
getTime(type) {
// 比如需要这样的格式 yyyy-MM-dd hh:mm:ss
var date = new Date();
let connector = "-";
if (type === "YYYY/MM/DD hh:mm:ss") {
connector = "/";
}
let Y = date.getFullYear() + connector;
let M =
(date.getMonth() + 1 < 10
? "0" + (date.getMonth() + 1)
: date.getMonth() + 1) + connector;
let D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
let h =
(date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
let m =
(date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
":";
let s =
date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
if (type === "YYYY-MM-DD") {
return Y + M + D;
} else if (type === "YYYY-MM-DD hh:mm:ss") {
return Y + M + D + h + m + s;
} else if (type === "YYYY/MM/DD hh:mm:ss") {
return Y + M + D + h + m + s;
}
// console.log(Y + M + D + h + m + s);
// 输出结果:2014-04-23 18:55:49
}
}
const timeUtil = (options) => new TimeUtil(options);
export default timeUtil;