@fr0st/datetime 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,163 @@
1
+ import { getOffsetTime } from './../helpers.js';
2
+ import { minimumDays, weekDay } from './../formatter/utility.js';
3
+ import { dayOfYear } from './../static/utility.js';
4
+
5
+ /**
6
+ * DateTime Attributes (Get)
7
+ */
8
+
9
+ /**
10
+ * Get the date of the month in current timeZone.
11
+ * @return {number} The date of the month.
12
+ */
13
+ export function getDate() {
14
+ return new Date(getOffsetTime(this)).getUTCDate();
15
+ };
16
+
17
+ /**
18
+ * Get the day of the week in current timeZone.
19
+ * @return {number} The day of the week. (0 - Sunday, 6 - Saturday)
20
+ */
21
+ export function getDay() {
22
+ return new Date(getOffsetTime(this)).getUTCDay();
23
+ };
24
+
25
+ /**
26
+ * Get the day of the year in current timeZone.
27
+ * @return {number} The day of the year. (1, 366)
28
+ */
29
+ export function getDayOfYear() {
30
+ return dayOfYear(
31
+ this.getYear(),
32
+ this.getMonth(),
33
+ this.getDate(),
34
+ );
35
+ };
36
+
37
+ /**
38
+ * Get the hours of the day in current timeZone.
39
+ * @return {number} The hours of the day. (0, 23)
40
+ */
41
+ export function getHours() {
42
+ return new Date(getOffsetTime(this)).getUTCHours();
43
+ };
44
+
45
+ /**
46
+ * Get the milliseconds in current timeZone.
47
+ * @return {number} The milliseconds.
48
+ */
49
+ export function getMilliseconds() {
50
+ return new Date(getOffsetTime(this)).getUTCMilliseconds();
51
+ };
52
+
53
+ /**
54
+ * Get the minutes in current timeZone.
55
+ * @return {number} The minutes. (0, 59)
56
+ */
57
+ export function getMinutes() {
58
+ return new Date(getOffsetTime(this)).getUTCMinutes();
59
+ };
60
+
61
+ /**
62
+ * Get the month in current timeZone.
63
+ * @return {number} The month. (1, 12)
64
+ */
65
+ export function getMonth() {
66
+ return new Date(getOffsetTime(this)).getUTCMonth() + 1;
67
+ };
68
+
69
+ /**
70
+ * Get the quarter of the year in current timeZone.
71
+ * @return {number} The quarter of the year. (1, 4)
72
+ */
73
+ export function getQuarter() {
74
+ return Math.ceil(this.getMonth() / 3);
75
+ };
76
+
77
+ /**
78
+ * Get the seconds in current timeZone.
79
+ * @return {number} The seconds. (0, 59)
80
+ */
81
+ export function getSeconds() {
82
+ return new Date(getOffsetTime(this)).getUTCSeconds();
83
+ };
84
+
85
+ /**
86
+ * Get the number of seconds since the UNIX epoch.
87
+ * @return {number} The number of seconds since the UNIX epoch.
88
+ */
89
+ export function getTimestamp() {
90
+ return Math.floor(this.getTime() / 1000);
91
+ };
92
+
93
+ /**
94
+ * Get the local week in current timeZone.
95
+ * @return {number} The local week. (1, 53)
96
+ */
97
+ export function getWeek() {
98
+ const thisWeek = this.startOf('day').setWeekDay(1);
99
+ const firstWeek = thisWeek.setWeek(1, 1);
100
+
101
+ return 1 +
102
+ (
103
+ (
104
+ (thisWeek - firstWeek) /
105
+ 604800000
106
+ ) | 0
107
+ );
108
+ };
109
+
110
+ /**
111
+ * Get the local day of the week in current timeZone.
112
+ * @return {number} The local day of the week. (1 - 7)
113
+ */
114
+ export function getWeekDay() {
115
+ return weekDay(
116
+ this.getLocale(),
117
+ this.getDay(),
118
+ );
119
+ };
120
+
121
+ /**
122
+ * Get the week day in month in current timeZone.
123
+ * @return {number} The week day in month.
124
+ */
125
+ export function getWeekDayInMonth() {
126
+ const thisWeek = this.getWeek();
127
+ const first = this.setDate(1);
128
+ const firstWeek = first.getWeek();
129
+ const offset = first.getWeekDay() > this.getWeekDay() ?
130
+ 0 : 1;
131
+ return firstWeek > thisWeek ?
132
+ thisWeek + offset :
133
+ thisWeek - firstWeek + offset;
134
+ };
135
+
136
+ /**
137
+ * Get the week of month in current timeZone.
138
+ * @return {number} The week of month.
139
+ */
140
+ export function getWeekOfMonth() {
141
+ const thisWeek = this.getWeek();
142
+ const firstWeek = this.setDate(1).getWeek();
143
+ return firstWeek > thisWeek ?
144
+ thisWeek + 1 :
145
+ thisWeek - firstWeek + 1;
146
+ };
147
+
148
+ /**
149
+ * Get the week year in current timeZone.
150
+ * @return {number} The week year.
151
+ */
152
+ export function getWeekYear() {
153
+ const minDays = minimumDays(this.getLocale());
154
+ return this.setWeekDay(7 - minDays + 1).getYear();
155
+ };
156
+
157
+ /**
158
+ * Get the year in current timeZone.
159
+ * @return {number} The year.
160
+ */
161
+ export function getYear() {
162
+ return new Date(getOffsetTime(this)).getUTCFullYear();
163
+ };
@@ -0,0 +1,281 @@
1
+ import DateTime from './../date-time.js';
2
+ import { getOffsetTime, setOffsetTime } from './../helpers.js';
3
+ import { config } from './../vars.js';
4
+ import { minimumDays } from './../formatter/utility.js';
5
+ import { daysInMonth } from './../static/utility.js';
6
+
7
+ /**
8
+ * DateTime Attributes (Set)
9
+ */
10
+
11
+ /**
12
+ * Set the date of the month in current timeZone.
13
+ * @param {number} date The date of the month.
14
+ * @return {DateTime} The DateTime object.
15
+ */
16
+ export function setDate(date) {
17
+ return setOffsetTime(
18
+ this,
19
+ new Date(getOffsetTime(this)).setUTCDate(date),
20
+ );
21
+ };
22
+
23
+ /**
24
+ * Set the day of the week in current timeZone.
25
+ * @param {number} day The day of the week. (0 - Sunday, 6 - Saturday)
26
+ * @return {DateTime} The DateTime object.
27
+ */
28
+ export function setDay(day) {
29
+ return setOffsetTime(
30
+ this,
31
+ new Date(getOffsetTime(this)).setUTCDate(
32
+ this.getDate() -
33
+ this.getDay() +
34
+ parseInt(day),
35
+ ),
36
+ );
37
+ };
38
+
39
+ /**
40
+ * Set the day of the year in current timeZone.
41
+ * @param {number} day The day of the year. (1, 366)
42
+ * @return {DateTime} The DateTime object.
43
+ */
44
+ export function setDayOfYear(day) {
45
+ return setOffsetTime(
46
+ this,
47
+ new Date(getOffsetTime(this)).setUTCMonth(
48
+ 0,
49
+ day,
50
+ ),
51
+ );
52
+ };
53
+
54
+ /**
55
+ * Set the hours in current timeZone (and optionally, minutes, seconds and milliseconds).
56
+ * @param {number} hours The hours. (0, 23)
57
+ * @param {number} [minutes] The minutes. (0, 59)
58
+ * @param {number} [seconds] The seconds. (0, 59)
59
+ * @param {number} [milliseconds] The milliseconds.
60
+ * @return {DateTime} The DateTime object.
61
+ */
62
+ export function setHours(...args) {
63
+ return setOffsetTime(
64
+ this,
65
+ new Date(getOffsetTime(this)).setUTCHours(...args),
66
+ );
67
+ };
68
+
69
+ /**
70
+ * Set the milliseconds in current timeZone.
71
+ * @param {number} milliseconds The milliseconds.
72
+ * @return {DateTime} The DateTime object.
73
+ */
74
+ export function setMilliseconds(milliseconds) {
75
+ return setOffsetTime(
76
+ this,
77
+ new Date(getOffsetTime(this)).setUTCMilliseconds(milliseconds),
78
+ );
79
+ };
80
+
81
+ /**
82
+ * Set the minutes in current timeZone (and optionally, seconds and milliseconds).
83
+ * @param {number} minutes The minutes. (0, 59)
84
+ * @param {number} [seconds] The seconds. (0, 59)
85
+ * @param {number} [milliseconds] The milliseconds.
86
+ * @return {DateTime} The DateTime object.
87
+ */
88
+ export function setMinutes(...args) {
89
+ return setOffsetTime(
90
+ this,
91
+ new Date(getOffsetTime(this)).setUTCMinutes(...args),
92
+ );
93
+ };
94
+
95
+ /**
96
+ * Set the month in current timeZone (and optionally, date).
97
+ * @param {number} month The month. (1, 12)
98
+ * @param {number|null} [date] The date of the month.
99
+ * @return {DateTime} The DateTime object.
100
+ */
101
+ export function setMonth(month, date = null) {
102
+ if (date === null) {
103
+ date = this.getDate();
104
+
105
+ if (config.clampDates) {
106
+ date = Math.min(
107
+ date,
108
+ daysInMonth(
109
+ this.getYear(),
110
+ month,
111
+ ),
112
+ );
113
+ }
114
+ }
115
+
116
+ return setOffsetTime(
117
+ this,
118
+ new Date(getOffsetTime(this)).setUTCMonth(
119
+ month - 1,
120
+ date,
121
+ ),
122
+ );
123
+ };
124
+
125
+ /**
126
+ * Set the quarter of the year in current timeZone.
127
+ * @param {number} quarter The quarter of the year. (1, 4)
128
+ * @return {DateTime} The DateTime object.
129
+ */
130
+ export function setQuarter(quarter) {
131
+ return setOffsetTime(
132
+ this,
133
+ new Date(getOffsetTime(this)).setUTCMonth(
134
+ quarter * 3 -
135
+ 3,
136
+ ),
137
+ );
138
+ };
139
+
140
+ /**
141
+ * Set the seconds in current timeZone (and optionally, milliseconds).
142
+ * @param {number} seconds The seconds. (0, 59)
143
+ * @param {number} [milliseconds] The milliseconds.
144
+ * @return {DateTime} The DateTime object.
145
+ */
146
+ export function setSeconds(...args) {
147
+ return setOffsetTime(
148
+ this,
149
+ new Date(getOffsetTime(this)).setUTCSeconds(...args),
150
+ );
151
+ };
152
+
153
+ /**
154
+ * Set the number of seconds since the UNIX epoch.
155
+ * @param {number} timestamp The number of seconds since the UNIX epoch.
156
+ * @return {DateTime} The DateTime object.
157
+ */
158
+ export function setTimestamp(timestamp) {
159
+ return this.setTime(timestamp * 1000);
160
+ };
161
+
162
+ /**
163
+ * Set the local day of the week in current timeZone (and optionally, day of the week).
164
+ * @param {number} week The local week.
165
+ * @param {number|null} [day] The local day of the week. (1 - 7)
166
+ * @return {DateTime} The DateTime object.
167
+ */
168
+ export function setWeek(week, day = null) {
169
+ if (day === null) {
170
+ day = this.getWeekDay();
171
+ }
172
+
173
+ const minDays = minimumDays(this.getLocale());
174
+ return this.setYear(this.getWeekYear(), 1, minDays + ((week - 1) * 7)).setWeekDay(day);
175
+ };
176
+
177
+ /**
178
+ * Set the local day of the week in current timeZone.
179
+ * @param {number} day The local day of the week. (1 - 7)
180
+ * @return {DateTime} The DateTime object.
181
+ */
182
+ export function setWeekDay(day) {
183
+ return setOffsetTime(
184
+ this,
185
+ new Date(getOffsetTime(this)).setUTCDate(
186
+ this.getDate() -
187
+ this.getWeekDay() +
188
+ parseInt(day),
189
+ ),
190
+ );
191
+ };
192
+
193
+ /**
194
+ * Set the week day in month in current timeZone.
195
+ * @param {number} week The week day in month.
196
+ * @return {DateTime} The DateTime object.
197
+ */
198
+ export function setWeekDayInMonth(week) {
199
+ return this.setDate(
200
+ this.getDate() +
201
+ (
202
+ week -
203
+ this.getWeekDayInMonth()
204
+ ) * 7,
205
+ );
206
+ };
207
+
208
+ /**
209
+ * Set the week of month in current timeZone.
210
+ * @param {number} week The week of month.
211
+ * @return {DateTime} The DateTime object.
212
+ */
213
+ export function setWeekOfMonth(week) {
214
+ return this.setDate(
215
+ this.getDate() +
216
+ (
217
+ week -
218
+ this.getWeekOfMonth()
219
+ ) * 7,
220
+ );
221
+ };
222
+
223
+ /**
224
+ * Set the local day of the week in current timeZone (and optionally, week and day of the week).
225
+ * @param {number} year The local year.
226
+ * @param {number|null} [week] The local week.
227
+ * @param {number|null} [day] The local day of the week. (1 - 7)
228
+ * @return {DateTime} The DateTime object.
229
+ */
230
+ export function setWeekYear(year, week = null, day = null) {
231
+ const minDays = minimumDays(this.getLocale());
232
+
233
+ if (week === null) {
234
+ week = Math.min(
235
+ this.getWeek(),
236
+ DateTime.fromArray([year, 1, minDays]).weeksInYear(),
237
+ );
238
+ }
239
+
240
+ if (day === null) {
241
+ day = this.getWeekDay();
242
+ }
243
+
244
+ return this.setYear(year, 1, minDays + ((week - 1) * 7)).setWeekDay(day);
245
+ };
246
+
247
+ /**
248
+ * Set the year in current timeZone (and optionally, month and date).
249
+ * @param {number} year The year.
250
+ * @param {number|null} [month] The month. (1, 12)
251
+ * @param {number|null} [date] The date of the month.
252
+ * @return {DateTime} The DateTime object.
253
+ */
254
+ export function setYear(year, month = null, date = null) {
255
+ if (month === null) {
256
+ month = this.getMonth();
257
+ }
258
+
259
+ if (date === null) {
260
+ date = this.getDate();
261
+
262
+ if (config.clampDates) {
263
+ date = Math.min(
264
+ date,
265
+ daysInMonth(
266
+ this.getYear(),
267
+ month,
268
+ ),
269
+ );
270
+ }
271
+ }
272
+
273
+ return setOffsetTime(
274
+ this,
275
+ new Date(getOffsetTime(this)).setUTCFullYear(
276
+ year,
277
+ month - 1,
278
+ date,
279
+ ),
280
+ );
281
+ };
@@ -0,0 +1,96 @@
1
+ import { modify } from './../helpers.js';
2
+ import { daysInMonth } from './../static/utility.js';
3
+
4
+ /**
5
+ * DateTime Manipulation
6
+ */
7
+
8
+ /**
9
+ * Add a duration to the date.
10
+ * @param {number} amount The amount to modify the date by.
11
+ * @param {string} timeUnit The unit of time.
12
+ * @return {DateTime} The DateTime object.
13
+ */
14
+ export function add(amount, timeUnit) {
15
+ return modify(this, amount, timeUnit);
16
+ };
17
+
18
+ /**
19
+ * Modify the DateTime by setting it to the end of a unit of time.
20
+ * @param {string} timeUnit The unit of time.
21
+ * @return {DateTime} The DateTime object.
22
+ */
23
+ export function endOf(timeUnit) {
24
+ timeUnit = timeUnit.toLowerCase();
25
+
26
+ switch (timeUnit) {
27
+ case 'second':
28
+ return this.setMilliseconds(999);
29
+ case 'minute':
30
+ return this.setSeconds(59, 999);
31
+ case 'hour':
32
+ return this.setMinutes(59, 59, 999);
33
+ case 'day':
34
+ return this.setHours(23, 59, 59, 999);
35
+ case 'week':
36
+ return this.setWeekDay(7)
37
+ .setHours(23, 59, 59, 999);
38
+ case 'month':
39
+ return this.setDate(this.daysInMonth())
40
+ .setHours(23, 59, 59, 999);
41
+ case 'quarter':
42
+ const month = this.getQuarter() * 3;
43
+ return this.setMonth(month, daysInMonth(this.getYear(), month))
44
+ .setHours(23, 59, 59, 999);
45
+ case 'year':
46
+ return this.setMonth(12, 31)
47
+ .setHours(23, 59, 59, 999);
48
+ default:
49
+ throw new Error('Invalid time unit supplied');
50
+ }
51
+ };
52
+
53
+ /**
54
+ * Modify the DateTime by setting it to the start of a unit of time.
55
+ * @param {string} timeUnit The unit of time.
56
+ * @return {DateTime} The DateTime object.
57
+ */
58
+ export function startOf(timeUnit) {
59
+ timeUnit = timeUnit.toLowerCase();
60
+
61
+ switch (timeUnit) {
62
+ case 'second':
63
+ return this.setMilliseconds(0);
64
+ case 'minute':
65
+ return this.setSeconds(0, 0);
66
+ case 'hour':
67
+ return this.setMinutes(0, 0, 0);
68
+ case 'day':
69
+ return this.setHours(0, 0, 0, 0);
70
+ case 'week':
71
+ return this.setWeekDay(1)
72
+ .setHours(0, 0, 0, 0);
73
+ case 'month':
74
+ return this.setDate(1)
75
+ .setHours(0, 0, 0, 0);
76
+ case 'quarter':
77
+ const month = this.getQuarter() * 3 - 2;
78
+ return this.setMonth(month, 1)
79
+ .setHours(0, 0, 0, 0);
80
+ case 'year':
81
+ return this.setMonth(1, 1)
82
+ .setHours(0, 0, 0, 0);
83
+ default:
84
+ throw new Error('Invalid time unit supplied');
85
+ }
86
+ };
87
+
88
+ /**
89
+ * Subtract a duration from the date.
90
+ * @param {number} amount The amount to modify the date by.
91
+ * @param {string} timeUnit The unit of time.
92
+ * @return {DateTime} The DateTime object.
93
+ */
94
+ export function sub(amount, timeUnit) {
95
+ return modify(this, -amount, timeUnit);
96
+ };
@@ -0,0 +1,89 @@
1
+ import { formats, formatTokenRegExp } from './../vars.js';
2
+ import tokens from './../formatter/tokens.js';
3
+
4
+ /**
5
+ * DateTime Output
6
+ */
7
+
8
+ /**
9
+ * Format the current date using a format string.
10
+ * @param {string} formatString The format string.
11
+ * @return {string} The formatted date string.
12
+ */
13
+ export function format(formatString) {
14
+ let match;
15
+ let output = '';
16
+
17
+ while (formatString && (match = formatString.match(formatTokenRegExp))) {
18
+ const token = match[1];
19
+ const position = match.index;
20
+ const length = match[0].length;
21
+
22
+ if (position) {
23
+ output += formatString.substring(0, position);
24
+ }
25
+
26
+ formatString = formatString.substring(position + length);
27
+
28
+ if (!token) {
29
+ output += match[0].slice(1, -1);
30
+ continue;
31
+ }
32
+
33
+ if (!(token in tokens)) {
34
+ throw new Error(`Invalid token in DateTime format: ${token}`);
35
+ }
36
+
37
+ output += tokens[token].output(this, length);
38
+ }
39
+
40
+ output += formatString;
41
+
42
+ return output;
43
+ };
44
+
45
+ /**
46
+ * Format the current date using "eee MMM dd yyyy".
47
+ * @return {string} The formatted date string.
48
+ */
49
+ export function toDateString() {
50
+ return this.format(formats.date);
51
+ };
52
+
53
+ /**
54
+ * Format the current date using "yyyy-MM-dd'THH:mm:ss.SSSSSSxxx".
55
+ * @return {string} The formatted date string.
56
+ */
57
+ export function toISOString() {
58
+ return this
59
+ .setLocale('en')
60
+ .setTimeZone('UTC')
61
+ .format(formats.rfc3339_extended);
62
+ };
63
+
64
+ /**
65
+ * Format the current date using "eee MMM dd yyyy HH:mm:ss xx (VV)".
66
+ * @return {string} The formatted date string.
67
+ */
68
+ export function toString() {
69
+ return this.format(formats.string);
70
+ };
71
+
72
+ /**
73
+ * Format the current date using "HH:mm:ss xx (VV)".
74
+ * @return {string} The formatted date string.
75
+ */
76
+ export function toTimeString() {
77
+ return this.format(formats.time);
78
+ };
79
+
80
+ /**
81
+ * Format the current date in UTC timeZone using "eee MMM dd yyyy HH:mm:ss xx (VV)".
82
+ * @return {string} The formatted date string.
83
+ */
84
+ export function toUTCString() {
85
+ return this
86
+ .setLocale('en')
87
+ .setTimeZone('UTC')
88
+ .toString();
89
+ };