-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.extenstions.ts
58 lines (48 loc) · 1.44 KB
/
date.extenstions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
export {};
declare global {
interface Date {
addHours(hours: number): Date;
addDays(days: number): Date;
isSameDay(otherDate: Date): boolean;
toWeekdayString(): string;
toTime(): string;
toShortISOString(): string;
isAfter(otherDate: Date): boolean;
isBefore(otherDate: Date): boolean;
}
}
Date.prototype.addHours = function (hours: number) {
this.setTime(this.getTime() + hours * 60 * 60 * 1000);
return this;
};
Date.prototype.addDays = function (days: number) {
this.setDate(this.getDate() + days);
return this;
};
Date.prototype.isSameDay = function (otherDate: Date): boolean {
var thisDateStr = this.toISOString().slice(0, 10);
var otherDateStr = otherDate.toISOString().slice(0, 10);
return thisDateStr === otherDateStr;
};
Date.prototype.toWeekdayString = function (): string {
return this.toLocaleDateString('en-us', {
day: '2-digit',
month: 'short',
weekday: 'long',
});
};
Date.prototype.toTime = function (): string {
return this.toLocaleTimeString('de-ch', {
hour: '2-digit',
minute: '2-digit',
});
};
Date.prototype.isAfter = function (otherDate: Date): boolean {
return this.getTime() < otherDate.getTime();
};
Date.prototype.isBefore = function (otherDate: Date): boolean {
return this.getTime() > otherDate.getTime();
};
Date.prototype.toShortISOString = function (): string {
return this.toISOString().split('T')[0];
};