const WEEKDAY = 7;
const run = Symbol('run');
const init = Symbol('init');
const DATE_TIMESTAMP = {
year: {
chs: '年',
timestamp: 12 * 30 * 24 * 60 * 60 * 1000
},
month: {
chs: '月',
timestamp: 30 * 24 * 60 * 60 * 1000
},
day: {
chs: '天',
timestamp: 24 * 60 * 60 * 1000
},
hour: {
chs: '小时',
timestamp: 60 * 60 * 1000
},
minute: {
chs: '分钟',
timestamp: 60 * 1000
},
second: {
chs: '秒',
timestamp: 1000
}
};
const weekdayEnum = [
'周日',
'周一',
'周二',
'周三',
'周四',
'周五',
'周六'
];
Object.freeze(DATE_TIMESTAMP);
Object.freeze(weekdayEnum);
class DateTime {
#dateTime;
constructor(date = new Date, format = 'yyyy-MM-dd') {
this.dateFormat = format;
this.#dateTime = DateTime.convertDate(date);
}
get today() {
return DateTime.format(new Date, this.dateFormat);
}
get toFormat() {
return DateTime.format(this.#dateTime, this.dateFormat);
}
get toDate() {
return this.#dateTime;
}
set setDate(date) {
this.#dateTime = DateTime.convertDate(date);
}
static isDate(date) {
return Object.prototype.toString.call(date) === '[object Date]';
}
static convertDate(date) {
if (!DateTime.isDate(date)) {
if (typeof date !== "number") {
date = String(date).replace(/[^\s\d:]/g, '/');
}
date = new Date(date);
if (date.toString() === 'Invalid Date') {
console.error('Invalid Date -- 日期格式不正确,仅支持时间戳,“年-月-日 时:分:秒等格式”');
throw new Error('Invalid Date -- 日期格式不正确,仅支持时间戳,“年-月-日 时:分:秒等格式”');
}
return date;
}
return date;
}
static timeFrom(lastTime, isDiff = false, fromTime = new Date) {
if (isDiff) {
let diff = /^\d+$/g.test(lastTime);
if (!diff) {
lastTime = Number(lastTime);
if (Number.isNaN(lastTime)) {
console.error('时间格式不正确,请输入正确的时间间隔');
throw new Error('时间格式不正确,请输入正确的时间间隔');
}
}
} else {
lastTime = DateTime.convertDate(lastTime).getTime()
}
let txt = '刚刚';
let suffix = '前';
let dateNow = fromTime.getTime();
let diffTime = isDiff ? lastTime : (dateNow - lastTime);
for (let key in DATE_TIMESTAMP) {
let value = DATE_TIMESTAMP[key];
if (diffTime >= value.timestamp && DATE_TIMESTAMP.hasOwnProperty(key)) {
txt = Math.floor(diffTime / value.timestamp) + `${value.chs === '月' ? '个月' : value.chs}` + suffix;
break;
}
}
return txt;
}
static format(date, format = 'yyyy/MM/dd hh:mm:ss') {
date = DateTime.convertDate(date);
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, String(date.getFullYear()).substr(4 - RegExp.$1.length));
}
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'wk': weekdayEnum[date.getDay()],
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
};
for (let k in o) {
if (new RegExp(`(${k})`).test(format)) {
let str = o[k] + '';
format = format.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : (str.padStart(2, '0')));
}
}
return format;
}
backToday() {
this.#dateTime = new Date;
return this;
}
addSeconds(secondNum) {
let second = Number.parseInt(secondNum);
this[run]({
second
});
return this;
}
addMinutes(minuteNum) {
let minute = Number.parseInt(minuteNum);
this[run]({
minute
});
return this;
}
addHours(hourNum) {
let hour = Number.parseInt(hourNum);
this[run]({
hour
});
return this;
}
addDays(dayNum) {
let day = Number.parseInt(dayNum);
this[run]({
day
});
return this;
}
addWeeks(weekNum) {
let week = Number.parseInt(weekNum);
this[run]({
week
});
return this;
}
addMonths(monthNum) {
let month = Number.parseInt(monthNum);
this[run]({
month
});
return this;
}
addYears(yearNum) {
let year = Number.parseInt(yearNum);
this[run]({
year
});
return this;
}
getWeekStart(isCurWeek = true, format = this.dateFormat) {
this[init](isCurWeek, format);
let wk = this.#dateTime.getDay() || WEEKDAY;
let toFormat = this[run]({
day: 1 - wk
}).toFormat;
this.backToday();
return toFormat;
}
getWeekEnd(isCurWeek = true, format = this.dateFormat) {
this[init](isCurWeek, format);
let wk = this.#dateTime.getDay() || WEEKDAY;
let toFormat = this[run]({
day: WEEKDAY - wk
}).toFormat;
this.backToday();
return toFormat;
}
getFirstDay(isCurMonth = true, format = this.dateFormat) {
this[init](isCurMonth, format);
let d = this.#dateTime.getDate();
let toFormat = this[run]({
day: 1 - d
}).toFormat;
this.backToday();
return toFormat;
}
getLastDay(isCurMonth = true, format = this.dateFormat) {
this[init](isCurMonth, format);
let d = this.#dateTime.getDate();
let toFormat = this.addMonths(1).addDays(0 - d).toFormat;
this.backToday();
return toFormat;
}
[run]({year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, week = 0}) {
let date = this.#dateTime;
let y = date.getFullYear() + year;
let M = date.getMonth() + month;
let d = date.getDate() + day;
d += week * WEEKDAY;
let h = date.getHours() + hour;
let m = date.getMinutes() + minute;
let s = date.getSeconds() + second;
this.#dateTime = new Date(y, M, d, h, m, s);
return this;
}
[init](isCurrent, format) {
if (isCurrent) {
this.#dateTime = new Date();
}
if (typeof isCurrent === 'string') {
this.dateFormat = isCurrent;
} else {
this.dateFormat = format;
}
}
}
let date = new DateTime()
date.addDays(2)
console.log(date.addMonths(2).toFormat)
console