export class DateTime {
timeofsubtle: number = 0;
constructor(time: Date | string) {
if (typeof time === "string") {
time = new Date(time);
}
this.timeofsubtle = time.getTime();
}
static parse(time: Date | string): DateTime {
return new this(time);
}
getTimeofsubtle(): number {
return this.timeofsubtle;
}
getTimestamp(): number {
return Number(this.timeofsubtle / 1000);
}
format(format: string = "YYYY-MM-DD HH:mm:ss"): string {
const date = new Date(this.timeofsubtle);
const config = {
YYYY: date.getFullYear(),
MM: date.getMonth() + 1,
DD: date.getDate(),
HH: date.getHours(),
mm: date.getMinutes(),
ss: date.getSeconds(),
} as any;
for (const key in config) {
config[key] = config[key].toString().padStart(key.length, "0");
format = format.replace(key, config[key] ?? "");
}
return format;
}
addMonths(months: number = 1): void {
const date = new Date(this.timeofsubtle);
let currentDay = date.getDate();
date.setDate(1);
date.setMonth(date.getMonth() + months);
this.timeofsubtle = date.getTime();
const maxDay = this.currentMonthMaxDay();
if (currentDay > maxDay) {
currentDay = maxDay;
}
date.setDate(currentDay);
this.timeofsubtle = date.getTime();
}
subMonths(months: number = 1): void {
const date = new Date(this.timeofsubtle);
let currentDay = date.getDate();
date.setDate(1);
date.setMonth(date.getMonth() - months);
this.timeofsubtle = date.getTime();
const maxDay = this.currentMonthMaxDay();
if (currentDay > maxDay) {
currentDay = maxDay;
}
date.setDate(currentDay);
this.timeofsubtle = date.getTime();
}
startMonths(format: string = "YYYY-MM-DD 00:00:00"): string {
return DateTime.parse(this.format("YYYY-MM-01 00:00:00")).format(format);
}
endMonths(format: string = "YYYY-MM-DD 23:59:59"): string {
const date = new Date(this.timeofsubtle);
date.setDate(this.currentMonthMaxDay());
return DateTime.parse(date).format(format);
}
currentMonthMaxDay(): number {
const currentMonth = DateTime.parse(this.format("YYYY-MM-01 00:00:00"));
const startTimeofsubtle = currentMonth.getTimeofsubtle();
const endDate = DateTime.parse(this.format("YYYY-MM-01 00:00:00")).format();
const newDate = new Date(endDate);
newDate.setMonth(newDate.getMonth() + 1);
const endTimeofsubtle = newDate.getTime();
const currentDays =
(endTimeofsubtle - startTimeofsubtle) / 1000 / 24 / 60 / 60;
return currentDays;
}
addHours(hours: number = 1): void {
this.timeofsubtle = this.timeofsubtle + 60 * 60 * 1000 * hours;
}
subHours(hours: number = 1): void {
this.timeofsubtle = this.timeofsubtle - 60 * 60 * 1000 * hours;
}
addDays(days: number = 1): void {
this.timeofsubtle = this.timeofsubtle + 24 * 60 * 60 * 1000 * days;
}
subDays(days: number = 1): void {
this.timeofsubtle = this.timeofsubtle - 24 * 60 * 60 * 1000 * days;
}
addMinutes(minutes: number = 1): void {
this.timeofsubtle = this.timeofsubtle + 60 * 1000 * minutes;
}
subMinutes(minutes: number = 1): void {
this.timeofsubtle = this.timeofsubtle + 60 * 1000 * minutes;
}
startDay(format: string = "YYYY-MM-DD HH:mm:ss"): string {
return DateTime.parse(this.format("YYYY-MM-DD 00:00:00")).format(format);
}
endDay(format: string = "YYYY-MM-DD HH:mm:ss"): string {
return DateTime.parse(this.format("YYYY-MM-DD 23:59:59")).format(format);
}
}