Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save briandilley/492895c6384f5eb7572006f2f7f7cdb7 to your computer and use it in GitHub Desktop.
Save briandilley/492895c6384f5eb7572006f2f7f7cdb7 to your computer and use it in GitHub Desktop.
export const ONE_MILLISECOND: number = 1;
export const ONE_SECOND: number = ONE_MILLISECOND * 1000;
export const ONE_MINUTE: number = ONE_SECOND * 60;
export const ONE_HOUR: number = ONE_MINUTE * 60;
Date.prototype.getUTCTime = getUTCTime;
Date.prototype.isEqual = isEqual;
Date.prototype.isBefore = isBefore;
Date.prototype.isAfter = isAfter;
Date.prototype.isOnOrBefore = isOnOrBefore;
Date.prototype.isOnOrAfter = isOnOrAfter;
Date.prototype.isToday = isToday;
Date.prototype.compareTo = compareTo;
declare global {
interface Date {
getUTCTime: typeof getUTCTime;
isEqual: typeof isEqual;
isBefore: typeof isBefore;
isAfter: typeof isAfter;
isOnOrBefore: typeof isOnOrBefore;
isOnOrAfter: typeof isOnOrAfter;
isToday: typeof isToday;
compareTo: typeof compareTo;
}
}
export function getUTCTime(this: Date): number {
return this.getTime() + (this.getTimezoneOffset() * 6000);
}
export function isEqual(this: Date, d: Date): boolean {
return d && this.getUTCTime() == d.getUTCTime();
}
export function isBefore(this: Date, d: Date): boolean {
return d && this.getUTCTime() < d.getUTCTime();
}
export function isAfter(this: Date, d: Date): boolean {
return d && this.getUTCTime() > d.getUTCTime();
}
export function isOnOrBefore(this: Date, d: Date): boolean {
return d && this.getUTCTime() <= d.getUTCTime();
}
export function isOnOrAfter(this: Date, d: Date): boolean {
return d && this.getUTCTime() >= d.getUTCTime();
}
export function isToday(this: Date): boolean {
let d: Date = new Date();
return d.getUTCFullYear() == this.getUTCFullYear()
&& d.getUTCMonth() == this.getUTCMonth()
&& d.getUTCDate() == this.getUTCDate()
}
export function compareTo(this: Date, d: Date): number {
let l: number = this.getUTCTime();
let r: number = d.getUTCTime();
if (l < r) {
return -1;
} else if (l > r) {
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment