In my search for a way to extend the Date prototype in Angular (Typescript), I stumbled upon a solution on GitHub that has proven to be effective.
date.extensions.ts
// DATE EXTENSIONS
// ================
declare global {
interface Date {
addDays(days: number, useThis?: boolean): Date;
isToday(): boolean;
clone(): Date;
isAnotherMonth(date: Date): boolean;
isWeekend(): boolean;
isSameDate(date: Date): boolean;
}
}
Date.prototype.addDays = (days: number): Date => {
if (!days) return this;
console.log(this);
let date = this;
date.setDate(date.getDate() + days);
return date;
};
Date.prototype.isToday = (): boolean => {
let today = new Date();
return this.isSameDate(today);
};
Date.prototype.clone = (): Date => {
return new Date(+this);
};
Date.prototype.isAnotherMonth = (date: Date): boolean => {
return date && this.getMonth() !== date.getMonth();
};
Date.prototype.isWeekend = (): boolean => {
return this.getDay() === 0 || this.getDay() === 6;
};
Date.prototype.isSameDate = (date: Date): boolean => {
return date && this.getFullYear() === date.getFullYear() && this.getMonth() === date.getMonth() && this.getDate() === date.getDate();
};
Ref: https://github.com/Microsoft/TypeScript/issues/7726#issuecomment-234469961
Question: Could someone explain the importance of including export {}
at the beginning of the TS file and why it is necessary?