|
@@ -53,7 +53,7 @@ export class Dayjs {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- startOf(unit: "year" | "month" | "day"): Dayjs {
|
|
|
|
|
|
+ startOf(unit?: "year" | "month" | "day"): Dayjs {
|
|
const newDate = new Date(this._date);
|
|
const newDate = new Date(this._date);
|
|
switch (unit) {
|
|
switch (unit) {
|
|
case "year":
|
|
case "year":
|
|
@@ -72,6 +72,25 @@ export class Dayjs {
|
|
return new Dayjs(newDate);
|
|
return new Dayjs(newDate);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ endOf(unit?: "year" | "month" | "day"): Dayjs {
|
|
|
|
+ const newDate = new Date(this._date);
|
|
|
|
+ switch (unit) {
|
|
|
|
+ case "year":
|
|
|
|
+ newDate.setMonth(11);
|
|
|
|
+ newDate.setDate(31);
|
|
|
|
+ newDate.setHours(23, 59, 59, 999);
|
|
|
|
+ break;
|
|
|
|
+ case "month":
|
|
|
|
+ newDate.setDate(new Date(newDate.getFullYear(), newDate.getMonth() + 1, 0).getDate());
|
|
|
|
+ newDate.setHours(23, 59, 59, 999);
|
|
|
|
+ break;
|
|
|
|
+ case "day":
|
|
|
|
+ newDate.setHours(23, 59, 59, 999);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ return new Dayjs(newDate);
|
|
|
|
+ }
|
|
|
|
+
|
|
add(value: number, unit: "year" | "month" | "day"): Dayjs {
|
|
add(value: number, unit: "year" | "month" | "day"): Dayjs {
|
|
const newDate = new Date(this._date);
|
|
const newDate = new Date(this._date);
|
|
switch (unit) {
|
|
switch (unit) {
|
|
@@ -88,6 +107,10 @@ export class Dayjs {
|
|
return new Dayjs(newDate);
|
|
return new Dayjs(newDate);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ subtract(value: number, unit: "year" | "month" | "day"): Dayjs {
|
|
|
|
+ return this.add(-value, unit);
|
|
|
|
+ }
|
|
|
|
+
|
|
format(formatStr: string): string {
|
|
format(formatStr: string): string {
|
|
const map: { [key: string]: number | string } = {
|
|
const map: { [key: string]: number | string } = {
|
|
YYYY: this._date.getFullYear(),
|
|
YYYY: this._date.getFullYear(),
|
|
@@ -177,6 +200,16 @@ export class Dayjs {
|
|
);
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ isBetween(
|
|
|
|
+ startDate: string | number | Date | Dayjs | null | undefined,
|
|
|
|
+ endDate: string | number | Date | Dayjs | null | undefined,
|
|
|
|
+ unit?: "year" | "month" | "day"
|
|
|
|
+ ): boolean {
|
|
|
|
+ const start = new Dayjs(startDate).startOf(unit);
|
|
|
|
+ const end = new Dayjs(endDate).endOf(unit);
|
|
|
|
+ return this.isAfter(start) && this.isBefore(end) || this.isSame(start) || this.isSame(end);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
const simpleDayjs = (date?: ConfigType): Dayjs => new Dayjs(date);
|
|
const simpleDayjs = (date?: ConfigType): Dayjs => new Dayjs(date);
|