smarttime/ts/smarttime.classes.extendeddate.ts

152 lines
3.8 KiB
TypeScript
Raw Permalink Normal View History

2022-11-21 08:14:32 +00:00
import * as plugins from './smarttime.plugins.js';
import * as units from './smarttime.units.js';
2018-10-21 10:12:44 +00:00
export type TAvailableZone = 'Europe/Berlin';
2019-06-18 12:38:57 +00:00
export interface IDateUnits {
year: number;
yearString: string;
month: number;
monthString: string;
monthName: string;
day: number;
dayString: string;
dayOfTheWeek: number;
dayOfTheWeekName: string;
}
export class ExtendedDate extends Date {
2019-06-18 12:38:57 +00:00
// STATIC factories
2019-06-18 09:58:36 +00:00
public static fromMillis(milliSeconds: number) {
return new ExtendedDate(milliSeconds);
}
2019-06-18 12:45:07 +00:00
public static fromDate(dateArg: Date) {
return new ExtendedDate(dateArg.getTime());
}
public static fromEuropeanDate(europeanDate: string) {
const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDate);
2020-05-27 16:59:26 +00:00
const date = new Date(
parseFloat(dateArray[3]), // year
2020-05-27 16:59:26 +00:00
parseFloat(dateArray[2]) - 1, // month
parseFloat(dateArray[1]) // day
);
2020-05-27 16:59:26 +00:00
const unixMilli = date.getTime();
return new ExtendedDate(unixMilli);
}
2019-06-18 12:38:57 +00:00
/**
* creates an Extended date from a hypedDate like "2018-03-28"
* @param dateString
*/
public static fromHyphedDate(dateString: string) {
// guards
// implementation
const dateMillis = new Date(dateString).getTime();
return new ExtendedDate(dateMillis);
}
/**
* Same as .fromEuropeanDate(), but accepts additional timeArg and zoneArg
*/
public static fromEuropeanDateAndTime(
europeanDateArg: string,
2019-06-18 09:58:36 +00:00
timeArg: string = '12:00:00',
2019-06-17 15:19:54 +00:00
zoneArg: TAvailableZone = 'Europe/Berlin'
) {
2019-06-18 12:38:57 +00:00
// guards
// implementation
2018-10-21 10:12:44 +00:00
const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDateArg);
const sliceDate = (dateString: string) => {
return `0${dateString}`.slice(-2);
};
2018-11-23 17:29:38 +00:00
const dateTimeString = `${dateArray[3]}-${sliceDate(dateArray[2])}-${sliceDate(
dateArray[1]
)}T${timeArg}`;
2020-05-27 16:59:26 +00:00
const date = plugins.dayjs(dateTimeString);
const unixMilli = date.toDate().getTime();
2018-10-21 10:12:44 +00:00
return new ExtendedDate(unixMilli);
}
2019-06-18 12:38:57 +00:00
// INSTANCE
2019-06-18 12:45:07 +00:00
public timezone: TAvailableZone;
2021-10-06 09:58:36 +00:00
constructor(unixMilli: number = Date.now()) {
super(unixMilli);
}
2019-06-18 12:38:57 +00:00
//
public exportToEuropeanDate() {
const units = this.exportToUnits();
return `${units.dayString}.${units.monthString}.${units.yearString}`;
}
2022-02-02 15:55:12 +00:00
public exportToHyphedSortableDate() {
const units = this.exportToUnits();
2022-02-02 16:03:58 +00:00
return `${units.yearString}-${units.monthString}-${units.dayString}`;
2022-02-02 15:55:12 +00:00
}
2019-06-18 12:38:57 +00:00
/**
* exports units
*/
public exportToUnits(): IDateUnits {
const monthsArray = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
2020-07-11 21:41:33 +00:00
'December',
2019-06-18 12:38:57 +00:00
];
const daysArray = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
2020-07-11 21:41:33 +00:00
'Sunday',
2019-06-18 12:38:57 +00:00
];
return {
year: this.getFullYear(),
yearString: `${this.getFullYear()}`,
month: this.getMonth() + 1,
2020-05-25 21:49:35 +00:00
monthString: ('0' + (this.getMonth() + 1)).slice(-2),
2019-06-18 12:38:57 +00:00
monthName: monthsArray[this.getMonth()],
day: this.getDate(),
2020-05-25 21:49:35 +00:00
dayString: ('0' + this.getDate()).slice(-2),
2019-06-18 12:38:57 +00:00
dayOfTheWeek: this.getDay(),
2020-07-11 21:41:33 +00:00
dayOfTheWeekName: daysArray[this.getDay()],
2019-06-18 12:38:57 +00:00
};
}
2020-10-05 17:48:41 +00:00
2022-11-21 08:14:32 +00:00
public format(formatArg: string) {
2020-10-05 17:48:41 +00:00
return plugins.dayjs(this.getTime()).format(formatArg);
}
2020-10-05 22:28:40 +00:00
2021-01-02 19:30:15 +00:00
/**
* boolean checks
*/
2022-11-21 08:14:32 +00:00
public isToday() {
2020-10-05 22:28:40 +00:00
return plugins.dayjs(this.getTime()).isToday();
}
2021-01-02 19:30:15 +00:00
public lessTimePassedToNow(unitArgs: units.IUnitCombinationArg): boolean {
const maxPassedUnixTime = units.getMilliSecondsFromUnits(unitArgs);
const actualPassedUnixTime = Date.now() - this.getTime();
return actualPassedUnixTime < maxPassedUnixTime;
}
public moreTimePassedToNow(unitArgs: units.IUnitCombinationArg) {
return !this.lessTimePassedToNow(unitArgs);
}
}