Skip to content

Commit 3d96c71

Browse files
committed
feat(date): addYear, addMonth, addDate, addHours, addMinutes, addSeconds, addMilliseconds
1 parent ffd75a8 commit 3d96c71

File tree

1 file changed

+82
-3
lines changed

1 file changed

+82
-3
lines changed

src/Date.ts

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,48 @@ define(Date, {
77
});
88

99
define(Date.prototype, {
10-
setTimezone: function (timezone: string) {
10+
setTimezone: function (this: Date, timezone: string) {
1111
return new Date(
1212
this.toLocaleString("en-US", {
1313
timeZone: timezone,
1414
})
1515
);
1616
},
17-
isInPast: function () {
17+
isInPast: function (this: Date) {
1818
const today = new Date();
1919
today.setHours(0, 0, 0, 0);
2020
return this < today;
2121
},
22+
addYear: function (this: Date, years: number, month: number = 0, date: number = 0) {
23+
return this.setFullYear(this.getFullYear() + years, this.getMonth() + month, this.getDate() + date);
24+
},
25+
addMonth: function (this: Date, months: number, date: number = 0) {
26+
return this.setMonth(this.getMonth() + months, this.getDate() + date);
27+
},
28+
addDate: function (this: Date, days: number) {
29+
return this.setDate(this.getDate() + days);
30+
},
31+
addHours: function (this: Date, hours: number, minutes: number = 0, seconds: number = 0, milliseconds: number = 0) {
32+
return this.setHours(
33+
this.getHours() + hours,
34+
this.getMinutes() + minutes,
35+
this.getSeconds() + seconds,
36+
this.getMilliseconds() + milliseconds
37+
);
38+
},
39+
addMinutes: function (this: Date, minutes: number, seconds: number = 0, milliseconds: number = 0) {
40+
return this.setMinutes(
41+
this.getMinutes() + minutes,
42+
this.getSeconds() + seconds,
43+
this.getMilliseconds() + milliseconds
44+
);
45+
},
46+
addSeconds: function (this: Date, seconds: number, milliseconds: number = 0) {
47+
return this.setSeconds(this.getSeconds() + seconds, this.getMilliseconds() + milliseconds);
48+
},
49+
addMilliseconds: function (this: Date, milliseconds: number) {
50+
return this.setMilliseconds(this.getMilliseconds() + milliseconds);
51+
},
2252
});
2353

2454
declare global {
@@ -27,7 +57,7 @@ declare global {
2757
* Returns the current timestamp as its representation in seconds
2858
* @returns {number} date in seconds
2959
* @example
30-
* Date.nowSeconds() // 1671621321
60+
* Date.nowSeconds()
3161
*/
3262
nowSeconds(): number;
3363
}
@@ -51,6 +81,55 @@ declare global {
5181
* new Date("2022-12-01").isInPast() // returns: true
5282
*/
5383
isInPast(): boolean;
84+
/**
85+
* Add a number of years to the current date
86+
* @returns {number} timestamp in milliseconds
87+
* @example
88+
* new Date().addYear(1)
89+
*/
90+
addYear(years: number, month?: number, date?: number): number;
91+
/**
92+
* Add a number of months to the current date
93+
* @returns {number} timestamp in milliseconds
94+
* @example
95+
* new Date().addMonth(1)
96+
*/
97+
addMonth(months: number, date?: number): number;
98+
/**
99+
* Add a number of days to the current date
100+
* @returns {number} timestamp in milliseconds
101+
* @example
102+
* new Date().addDate(1)
103+
*/
104+
addDate(days: number): number;
105+
/**
106+
* Add a number of hours to the current date
107+
* @returns {number} timestamp in milliseconds
108+
* @example
109+
* new Date().addHours(1)
110+
*/
111+
addHours(hours: number, minutes?: number, seconds?: number, milliseconds?: number): number;
112+
/**
113+
* Add a number of minutes to the current date
114+
* @returns {number} timestamp in milliseconds
115+
* @example
116+
* new Date().addMinutes(1)
117+
*/
118+
addMinutes(minutes: number, seconds?: number, milliseconds?: number): number;
119+
/**
120+
* Add a number of seconds to the current date
121+
* @returns {number} timestamp in milliseconds
122+
* @example
123+
* new Date().addSeconds(1)
124+
*/
125+
addSeconds(seconds: number, milliseconds?: number): number;
126+
/**
127+
* Add a number of milliseconds to the current date
128+
* @returns {number} timestamp in milliseconds
129+
* @example
130+
* new Date().addMilliseconds(1)
131+
*/
132+
addMilliseconds(milliseconds: number): number;
54133
}
55134
}
56135

0 commit comments

Comments
 (0)