atomic-fns 0.3.3-beta → 0.3.4
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.
- package/core/collections/index.js +5 -5
- package/core/collections/index.ts +5 -5
- package/core/functools/index.d.ts +2 -2
- package/core/functools/index.ts +6 -3
- package/core/globals/index.d.ts +22 -3
- package/core/globals/index.js +28 -5
- package/core/globals/index.ts +39 -6
- package/core/i18n/date/format.d.ts +16 -0
- package/core/i18n/date/format.js +307 -0
- package/core/i18n/date/format.ts +343 -0
- package/core/i18n/date/index.d.ts +223 -0
- package/core/i18n/date/index.js +387 -0
- package/core/i18n/date/index.ts +471 -0
- package/core/i18n/date/test.html +67 -0
- package/core/i18n/index.d.ts +7 -0
- package/core/i18n/index.js +7 -0
- package/core/i18n/index.ts +8 -0
- package/core/index.d.ts +1 -0
- package/core/index.js +1 -0
- package/core/index.ts +1 -0
- package/core/operators/index.d.ts +1 -1
- package/core/operators/index.js +35 -5
- package/core/operators/index.ts +34 -6
- package/package.json +11 -11
- package/tests/globals.spec.ts +9 -9
- package/tsconfig.json +2 -1
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
export declare type DateParts = {
|
|
2
|
+
year?: number;
|
|
3
|
+
month?: number;
|
|
4
|
+
day?: number;
|
|
5
|
+
hour?: number;
|
|
6
|
+
minute?: number;
|
|
7
|
+
second?: number;
|
|
8
|
+
millisecond?: number;
|
|
9
|
+
};
|
|
10
|
+
export declare type DateLike = number | string | Date | DateParts;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a date tied to a given locale (default system locale) which can be formatted in that locale's language using the native {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl Intl Apis} directly or using a formatting string compatible with `strftime`.
|
|
13
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl Intl Apis}
|
|
14
|
+
* @see {@link https://strftime.org/ strftime format}
|
|
15
|
+
*/
|
|
16
|
+
export declare class IntlDate {
|
|
17
|
+
private self;
|
|
18
|
+
readonly year: number;
|
|
19
|
+
readonly M: number;
|
|
20
|
+
readonly day: number;
|
|
21
|
+
readonly weekDay: number;
|
|
22
|
+
readonly hour: number;
|
|
23
|
+
readonly minute: number;
|
|
24
|
+
readonly second: number;
|
|
25
|
+
readonly millisecond: number;
|
|
26
|
+
readonly locale: string;
|
|
27
|
+
/** Returns the current UTC date and time. */
|
|
28
|
+
static UTC(obj?: DateLike): IntlDate;
|
|
29
|
+
/** Returns the current local date and time.*/
|
|
30
|
+
static now(): IntlDate;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new `IntlDate` tied to the specified locale (default is system locale).
|
|
33
|
+
* **Note:** The allowed values for `month` start at 1, which is different from legacy `Date`
|
|
34
|
+
* @param {DateLike | IntlDate} obj The date value (default is now)
|
|
35
|
+
* @param {{locale?: string, utc?: boolean}} opts
|
|
36
|
+
* @returns
|
|
37
|
+
*/
|
|
38
|
+
constructor(obj?: DateLike | IntlDate, { locale, utc }?: any);
|
|
39
|
+
isValid(): boolean;
|
|
40
|
+
/** Get the this date's month as a number from 1-12. */
|
|
41
|
+
get month(): number;
|
|
42
|
+
/** Get the number of days in this date's month. */
|
|
43
|
+
daysInMonth(): number | null;
|
|
44
|
+
/** Get the number of days in this date's year. */
|
|
45
|
+
daysInYear(): 366 | 365;
|
|
46
|
+
/** Returns `true` if this date's year is a leap year, otherwise `false`. */
|
|
47
|
+
isLeapYear(): boolean;
|
|
48
|
+
/** Gets the number of weeks according to locale in the current moment's year. */
|
|
49
|
+
weeksInYear(): 53 | 52;
|
|
50
|
+
/**
|
|
51
|
+
* Returns a localized string representation of this date.
|
|
52
|
+
* @param {Intl.DateTimeFormatOptions} opts The options to use for the format
|
|
53
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat Intl.DateTimeFormat()}
|
|
54
|
+
*/
|
|
55
|
+
toString(opts?: Intl.DateTimeFormatOptions): string;
|
|
56
|
+
/**
|
|
57
|
+
* Returns a localized string representation of this date, according to the given format string. Format codes use the same specification as C / Python's strftime().
|
|
58
|
+
* @param {string} str The format string to use
|
|
59
|
+
* @see {@link https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes Format Codes}
|
|
60
|
+
* @example
|
|
61
|
+
```js
|
|
62
|
+
new IntlDate().format('%m/%d/%y') // '10/31/2022'
|
|
63
|
+
```
|
|
64
|
+
*/
|
|
65
|
+
format(str: string): string;
|
|
66
|
+
/**
|
|
67
|
+
* Returns a new copy of the native `Date` object used by this instance.
|
|
68
|
+
* @returns {Date} A new `Date` object
|
|
69
|
+
*/
|
|
70
|
+
toDate(): Date;
|
|
71
|
+
/**
|
|
72
|
+
* Returns the timestamp representing the UTC equivalent of this date.
|
|
73
|
+
* @returns {number} The UTC timestamp of this date.
|
|
74
|
+
*/
|
|
75
|
+
toUTC(): number;
|
|
76
|
+
/**
|
|
77
|
+
* Returns the number of seconds since the Unix Epoch (January 1, 1970 UTC).
|
|
78
|
+
* @see {@link IntlDate.timestamp}
|
|
79
|
+
* @see {@link IntlDate.unix}
|
|
80
|
+
* @example
|
|
81
|
+
```js
|
|
82
|
+
new IntlDate().toSeconds() // 1318874398
|
|
83
|
+
```
|
|
84
|
+
*/
|
|
85
|
+
toSeconds(): number;
|
|
86
|
+
/**
|
|
87
|
+
* Returns the number of seconds since the Unix Epoch (January 1, 1970 UTC).
|
|
88
|
+
* @see {@link IntlDate.toSeconds}
|
|
89
|
+
* @see {@link IntlDate.unix}
|
|
90
|
+
* @example
|
|
91
|
+
```js
|
|
92
|
+
new IntlDate().timestamp() // 1318874398
|
|
93
|
+
```
|
|
94
|
+
*/
|
|
95
|
+
timestamp(): number;
|
|
96
|
+
/**
|
|
97
|
+
* Returns the number of seconds since the Unix Epoch (January 1, 1970 UTC).
|
|
98
|
+
* @see {@link IntlDate.toSeconds}
|
|
99
|
+
* @see {@link IntlDate.timestamp}
|
|
100
|
+
* @example
|
|
101
|
+
```js
|
|
102
|
+
new IntlDate().unix() // 1318874398
|
|
103
|
+
```
|
|
104
|
+
*/
|
|
105
|
+
unix(): number;
|
|
106
|
+
/** Formats a string to the ISO8601 standard.
|
|
107
|
+
* @example
|
|
108
|
+
```js
|
|
109
|
+
new IntlDate().toISOString() // '2022-10-31T22:44:30.652Z'
|
|
110
|
+
```
|
|
111
|
+
*/
|
|
112
|
+
toISOString(): string;
|
|
113
|
+
/** Returns the date part formatted as ISO8601.
|
|
114
|
+
* @example
|
|
115
|
+
```js
|
|
116
|
+
new IntlDate().toISODate() // '2022-10-31'
|
|
117
|
+
```
|
|
118
|
+
*/
|
|
119
|
+
toISODate(): string;
|
|
120
|
+
/** Returns the time part formatted as ISO8601.
|
|
121
|
+
* @example
|
|
122
|
+
```js
|
|
123
|
+
new IntlDate().toISOTime() // 'T22:44:30.652Z'
|
|
124
|
+
```
|
|
125
|
+
*/
|
|
126
|
+
toISOTime(): string;
|
|
127
|
+
/**
|
|
128
|
+
* Returns an object containing year, month, day-of-month, hours, minutes, seconds, milliseconds.
|
|
129
|
+
* @returns {Object} An object like `{year, month, date, hours, minutes, seconds, ms}`
|
|
130
|
+
*/
|
|
131
|
+
toObject(): DateParts;
|
|
132
|
+
/** Returns the number of milliseconds since the Unix Epoch (January 1, 1970 UTC) */
|
|
133
|
+
valueOf(): number;
|
|
134
|
+
/** Returns the timezone string name */
|
|
135
|
+
zoneName(): string;
|
|
136
|
+
/** Returns the timezone GMT offset name */
|
|
137
|
+
offset(): string;
|
|
138
|
+
/**
|
|
139
|
+
* Check if this date is before another date. The other value will be parsed as an `IntlDate` if not already so.
|
|
140
|
+
* @param {string | number | Object | Date | IntlDate} other Another date or date like object
|
|
141
|
+
* @returns {boolean} Returns `true` if this date is before the given value
|
|
142
|
+
*/
|
|
143
|
+
isBefore(other: DateLike | IntlDate): boolean;
|
|
144
|
+
/**
|
|
145
|
+
* Check if this date is strictly after the given `start` and before `stop` dates. The values will be parsed as an `IntlDate` if not already so.
|
|
146
|
+
* @param {string | number | Object | Date | IntlDate} start The smaller date
|
|
147
|
+
* @param {string | number | Object | Date | IntlDate} stop The larger date
|
|
148
|
+
* @returns {boolean} Returns `true` if this date is greater than `start` and less than `stop`.
|
|
149
|
+
*/
|
|
150
|
+
isBetween(start: DateLike | IntlDate, stop: DateLike | IntlDate): boolean;
|
|
151
|
+
/**
|
|
152
|
+
* Check if this date is after another date. The other value will be parsed as an `IntlDate` if not already so.
|
|
153
|
+
* @param {string | number | DateParts | Date | IntlDate} other Another date or date like object
|
|
154
|
+
* @returns {boolean} Returns `true` if this date is after the given value
|
|
155
|
+
*/
|
|
156
|
+
isAfter(other: DateLike | IntlDate): boolean;
|
|
157
|
+
/**
|
|
158
|
+
* Check if this date is the same as `other`.
|
|
159
|
+
* @param other Another date object
|
|
160
|
+
* @returns {boolean} Returns `true` if this date is the same as other
|
|
161
|
+
*/
|
|
162
|
+
isSame(other: Date | IntlDate): boolean;
|
|
163
|
+
/**
|
|
164
|
+
* Returns the minimum (most distant past) of the given date values.
|
|
165
|
+
* @param {Array<Date | IntlDate>} dates
|
|
166
|
+
* @returns {Date | IntlDate} The smallest date of `dates`
|
|
167
|
+
* @see {@link minDate}
|
|
168
|
+
*/
|
|
169
|
+
static min(...dates: Array<Date | IntlDate>): Date | IntlDate;
|
|
170
|
+
/**
|
|
171
|
+
* Returns the maximum (most distant future) of the given date values.
|
|
172
|
+
* @param {Array<Date | IntlDate>} dates
|
|
173
|
+
* @returns {Date | IntlDate} The largest date of `dates`
|
|
174
|
+
* @see {@link maxDate}
|
|
175
|
+
*/
|
|
176
|
+
static max(...dates: Array<Date | IntlDate>): Date | IntlDate;
|
|
177
|
+
lt(other: Date | IntlDate): boolean;
|
|
178
|
+
eq(other: Date | IntlDate): boolean;
|
|
179
|
+
compare(other: Date | IntlDate): 0 | 1 | -1;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Converts the given date to a UTC timestamp.
|
|
183
|
+
* @param {Date} date
|
|
184
|
+
* @returns {number} The milliseconds between the specified date and the UTC Epoch date.
|
|
185
|
+
*/
|
|
186
|
+
export declare function dateToUTC(date: Date): number;
|
|
187
|
+
/**
|
|
188
|
+
* Returns the minimum (most distant past) of the given date values.
|
|
189
|
+
* @param {Array<Date | IntlDate>} dates
|
|
190
|
+
* @returns {Date | IntlDate} The smallest date of `dates`
|
|
191
|
+
*/
|
|
192
|
+
export declare function minDate(...dates: Array<Date | IntlDate>): Date | IntlDate;
|
|
193
|
+
/**
|
|
194
|
+
* Returns the maximum (most distant future) of the given date values.
|
|
195
|
+
* @param {Array<Date | IntlDate>} dates
|
|
196
|
+
* @returns {Date | IntlDate} The largest date of `dates`
|
|
197
|
+
*/
|
|
198
|
+
export declare function maxDate(...dates: Array<Date | IntlDate>): Date | IntlDate;
|
|
199
|
+
/**
|
|
200
|
+
* Returns `true` if the year is a leap year, otherwise `false`.
|
|
201
|
+
* @param year The date year
|
|
202
|
+
* @returns {boolean} `true` if is a leap year
|
|
203
|
+
*/
|
|
204
|
+
export declare function isLeapYear(year: number): boolean;
|
|
205
|
+
/**
|
|
206
|
+
* Get the number of days in the given year.
|
|
207
|
+
* @param year The date year
|
|
208
|
+
* @returns {number} Number of days in year
|
|
209
|
+
*/
|
|
210
|
+
export declare function daysInYear(year: number): 366 | 365;
|
|
211
|
+
/**
|
|
212
|
+
* Get the number of days in the given month and year.
|
|
213
|
+
* @param year The date year
|
|
214
|
+
* @param month The date month
|
|
215
|
+
* @returns {number} Number of days in month
|
|
216
|
+
*/
|
|
217
|
+
export declare function daysInMonth(year: number, month: number): number | null;
|
|
218
|
+
/**
|
|
219
|
+
* Gets the number of weeks according to locale in the current moment's year.
|
|
220
|
+
* @param {number} weekYear The the date year
|
|
221
|
+
* @returns {number}
|
|
222
|
+
*/
|
|
223
|
+
export declare function weeksInYear(weekYear: number): 53 | 52;
|
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
import { isObject } from '../../globals/index.js';
|
|
2
|
+
import { format, getDate } from './format.js';
|
|
3
|
+
let globalOptions;
|
|
4
|
+
const systemOptions = () => {
|
|
5
|
+
if (!globalOptions)
|
|
6
|
+
globalOptions = Intl.DateTimeFormat().resolvedOptions();
|
|
7
|
+
return globalOptions;
|
|
8
|
+
};
|
|
9
|
+
const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ';
|
|
10
|
+
const INVALID_DATE_STRING = 'Invalid Date';
|
|
11
|
+
/**
|
|
12
|
+
* Creates a date tied to a given locale (default system locale) which can be formatted in that locale's language using the native {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl Intl Apis} directly or using a formatting string compatible with `strftime`.
|
|
13
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl Intl Apis}
|
|
14
|
+
* @see {@link https://strftime.org/ strftime format}
|
|
15
|
+
*/
|
|
16
|
+
export class IntlDate {
|
|
17
|
+
self;
|
|
18
|
+
year;
|
|
19
|
+
M;
|
|
20
|
+
day;
|
|
21
|
+
weekDay;
|
|
22
|
+
hour;
|
|
23
|
+
minute;
|
|
24
|
+
second;
|
|
25
|
+
millisecond;
|
|
26
|
+
locale;
|
|
27
|
+
/** Returns the current UTC date and time. */
|
|
28
|
+
static UTC(obj = {}) {
|
|
29
|
+
return new IntlDate(obj, { utc: true });
|
|
30
|
+
}
|
|
31
|
+
/** Returns the current local date and time.*/
|
|
32
|
+
static now() {
|
|
33
|
+
return new IntlDate();
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Creates a new `IntlDate` tied to the specified locale (default is system locale).
|
|
37
|
+
* **Note:** The allowed values for `month` start at 1, which is different from legacy `Date`
|
|
38
|
+
* @param {DateLike | IntlDate} obj The date value (default is now)
|
|
39
|
+
* @param {{locale?: string, utc?: boolean}} opts
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
constructor(obj, { locale, utc } = {}) {
|
|
43
|
+
// Set the default DateTime locale
|
|
44
|
+
this.locale = locale || systemOptions().locale;
|
|
45
|
+
if (obj instanceof IntlDate)
|
|
46
|
+
obj = obj.self;
|
|
47
|
+
if (typeof obj === 'string' || typeof obj === 'number' || obj instanceof Date) {
|
|
48
|
+
this.self = getDate(obj, utc);
|
|
49
|
+
}
|
|
50
|
+
else if (isObject(obj)) {
|
|
51
|
+
let { year, month, day, hour, minute, second, millisecond } = obj;
|
|
52
|
+
// NOTE month is from 1-12, not from 0-11
|
|
53
|
+
if (typeof month === 'number')
|
|
54
|
+
month -= 1;
|
|
55
|
+
if (utc) {
|
|
56
|
+
// from UTC to current
|
|
57
|
+
this.self = new Date(Date.UTC(year, month, day, hour, minute, second, millisecond));
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
this.self = new Date(year, month, day, hour, minute, second, millisecond);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
this.self = new Date();
|
|
65
|
+
}
|
|
66
|
+
this.year = this.self.getFullYear();
|
|
67
|
+
this.M = this.self.getMonth();
|
|
68
|
+
this.weekDay = this.self.getDay();
|
|
69
|
+
this.day = this.self.getDate();
|
|
70
|
+
this.hour = this.self.getHours();
|
|
71
|
+
this.minute = this.self.getMinutes();
|
|
72
|
+
this.second = this.self.getSeconds();
|
|
73
|
+
this.millisecond = this.self.getMilliseconds();
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
isValid() {
|
|
77
|
+
return this.self.toString() !== INVALID_DATE_STRING;
|
|
78
|
+
}
|
|
79
|
+
/** Get the this date's month as a number from 1-12. */
|
|
80
|
+
get month() {
|
|
81
|
+
return this.M + 1;
|
|
82
|
+
}
|
|
83
|
+
/** Get the number of days in this date's month. */
|
|
84
|
+
daysInMonth() {
|
|
85
|
+
return daysInMonth(this.year, this.M);
|
|
86
|
+
}
|
|
87
|
+
/** Get the number of days in this date's year. */
|
|
88
|
+
daysInYear() {
|
|
89
|
+
return daysInYear(this.year);
|
|
90
|
+
}
|
|
91
|
+
/** Returns `true` if this date's year is a leap year, otherwise `false`. */
|
|
92
|
+
isLeapYear() {
|
|
93
|
+
return isLeapYear(this.year);
|
|
94
|
+
}
|
|
95
|
+
/** Gets the number of weeks according to locale in the current moment's year. */
|
|
96
|
+
weeksInYear() {
|
|
97
|
+
return weeksInYear(this.year);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Returns a localized string representation of this date.
|
|
101
|
+
* @param {Intl.DateTimeFormatOptions} opts The options to use for the format
|
|
102
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat Intl.DateTimeFormat()}
|
|
103
|
+
*/
|
|
104
|
+
toString(opts) {
|
|
105
|
+
return this.self.toLocaleDateString(this.locale, opts);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Returns a localized string representation of this date, according to the given format string. Format codes use the same specification as C / Python's strftime().
|
|
109
|
+
* @param {string} str The format string to use
|
|
110
|
+
* @see {@link https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes Format Codes}
|
|
111
|
+
* @example
|
|
112
|
+
```js
|
|
113
|
+
new IntlDate().format('%m/%d/%y') // '10/31/2022'
|
|
114
|
+
```
|
|
115
|
+
*/
|
|
116
|
+
format(str) {
|
|
117
|
+
return format(str, this.self, this.locale);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Returns a new copy of the native `Date` object used by this instance.
|
|
121
|
+
* @returns {Date} A new `Date` object
|
|
122
|
+
*/
|
|
123
|
+
toDate() {
|
|
124
|
+
return new Date(this.isValid() ? this.self : NaN);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Returns the timestamp representing the UTC equivalent of this date.
|
|
128
|
+
* @returns {number} The UTC timestamp of this date.
|
|
129
|
+
*/
|
|
130
|
+
toUTC() {
|
|
131
|
+
return dateToUTC(this.self);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Returns the number of seconds since the Unix Epoch (January 1, 1970 UTC).
|
|
135
|
+
* @see {@link IntlDate.timestamp}
|
|
136
|
+
* @see {@link IntlDate.unix}
|
|
137
|
+
* @example
|
|
138
|
+
```js
|
|
139
|
+
new IntlDate().toSeconds() // 1318874398
|
|
140
|
+
```
|
|
141
|
+
*/
|
|
142
|
+
toSeconds() {
|
|
143
|
+
return Math.trunc(this.valueOf() / 1000);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Returns the number of seconds since the Unix Epoch (January 1, 1970 UTC).
|
|
147
|
+
* @see {@link IntlDate.toSeconds}
|
|
148
|
+
* @see {@link IntlDate.unix}
|
|
149
|
+
* @example
|
|
150
|
+
```js
|
|
151
|
+
new IntlDate().timestamp() // 1318874398
|
|
152
|
+
```
|
|
153
|
+
*/
|
|
154
|
+
timestamp() {
|
|
155
|
+
return this.toSeconds();
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Returns the number of seconds since the Unix Epoch (January 1, 1970 UTC).
|
|
159
|
+
* @see {@link IntlDate.toSeconds}
|
|
160
|
+
* @see {@link IntlDate.timestamp}
|
|
161
|
+
* @example
|
|
162
|
+
```js
|
|
163
|
+
new IntlDate().unix() // 1318874398
|
|
164
|
+
```
|
|
165
|
+
*/
|
|
166
|
+
unix() {
|
|
167
|
+
return this.toSeconds();
|
|
168
|
+
}
|
|
169
|
+
/** Formats a string to the ISO8601 standard.
|
|
170
|
+
* @example
|
|
171
|
+
```js
|
|
172
|
+
new IntlDate().toISOString() // '2022-10-31T22:44:30.652Z'
|
|
173
|
+
```
|
|
174
|
+
*/
|
|
175
|
+
toISOString() {
|
|
176
|
+
return this.self.toISOString();
|
|
177
|
+
}
|
|
178
|
+
/** Returns the date part formatted as ISO8601.
|
|
179
|
+
* @example
|
|
180
|
+
```js
|
|
181
|
+
new IntlDate().toISODate() // '2022-10-31'
|
|
182
|
+
```
|
|
183
|
+
*/
|
|
184
|
+
toISODate() {
|
|
185
|
+
return this.self.toISOString().slice(0, 10);
|
|
186
|
+
}
|
|
187
|
+
/** Returns the time part formatted as ISO8601.
|
|
188
|
+
* @example
|
|
189
|
+
```js
|
|
190
|
+
new IntlDate().toISOTime() // 'T22:44:30.652Z'
|
|
191
|
+
```
|
|
192
|
+
*/
|
|
193
|
+
toISOTime() {
|
|
194
|
+
return this.self.toISOString().slice(11);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Returns an object containing year, month, day-of-month, hours, minutes, seconds, milliseconds.
|
|
198
|
+
* @returns {Object} An object like `{year, month, date, hours, minutes, seconds, ms}`
|
|
199
|
+
*/
|
|
200
|
+
toObject() {
|
|
201
|
+
return {
|
|
202
|
+
year: this.year,
|
|
203
|
+
month: this.month,
|
|
204
|
+
day: this.day,
|
|
205
|
+
hour: this.hour,
|
|
206
|
+
minute: this.M,
|
|
207
|
+
second: this.second,
|
|
208
|
+
millisecond: this.millisecond
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
/** Returns the number of milliseconds since the Unix Epoch (January 1, 1970 UTC) */
|
|
212
|
+
valueOf() {
|
|
213
|
+
return this.isValid() ? this.self.getTime() : NaN;
|
|
214
|
+
}
|
|
215
|
+
/** Returns the timezone string name */
|
|
216
|
+
zoneName() {
|
|
217
|
+
return this.format('%Z');
|
|
218
|
+
}
|
|
219
|
+
/** Returns the timezone GMT offset name */
|
|
220
|
+
offset() {
|
|
221
|
+
return this.format('%z');
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Check if this date is before another date. The other value will be parsed as an `IntlDate` if not already so.
|
|
225
|
+
* @param {string | number | Object | Date | IntlDate} other Another date or date like object
|
|
226
|
+
* @returns {boolean} Returns `true` if this date is before the given value
|
|
227
|
+
*/
|
|
228
|
+
isBefore(other) {
|
|
229
|
+
if (!this.isValid())
|
|
230
|
+
return false;
|
|
231
|
+
if (other instanceof IntlDate) {
|
|
232
|
+
return this.valueOf() < other.valueOf();
|
|
233
|
+
}
|
|
234
|
+
return this.valueOf() < new IntlDate(other).valueOf();
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Check if this date is strictly after the given `start` and before `stop` dates. The values will be parsed as an `IntlDate` if not already so.
|
|
238
|
+
* @param {string | number | Object | Date | IntlDate} start The smaller date
|
|
239
|
+
* @param {string | number | Object | Date | IntlDate} stop The larger date
|
|
240
|
+
* @returns {boolean} Returns `true` if this date is greater than `start` and less than `stop`.
|
|
241
|
+
*/
|
|
242
|
+
isBetween(start, stop) {
|
|
243
|
+
return this.isAfter(start) && this.isBefore(stop);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Check if this date is after another date. The other value will be parsed as an `IntlDate` if not already so.
|
|
247
|
+
* @param {string | number | DateParts | Date | IntlDate} other Another date or date like object
|
|
248
|
+
* @returns {boolean} Returns `true` if this date is after the given value
|
|
249
|
+
*/
|
|
250
|
+
isAfter(other) {
|
|
251
|
+
if (!this.isValid())
|
|
252
|
+
return false;
|
|
253
|
+
if (other instanceof IntlDate) {
|
|
254
|
+
return this.valueOf() > other.valueOf();
|
|
255
|
+
}
|
|
256
|
+
return this.valueOf() > new IntlDate(other).valueOf();
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Check if this date is the same as `other`.
|
|
260
|
+
* @param other Another date object
|
|
261
|
+
* @returns {boolean} Returns `true` if this date is the same as other
|
|
262
|
+
*/
|
|
263
|
+
isSame(other) {
|
|
264
|
+
if (other instanceof IntlDate) {
|
|
265
|
+
return (this.isValid() &&
|
|
266
|
+
other.isValid() &&
|
|
267
|
+
this.valueOf() === other.valueOf() &&
|
|
268
|
+
this.locale === other.locale);
|
|
269
|
+
}
|
|
270
|
+
return this.valueOf() === other.valueOf();
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Returns the minimum (most distant past) of the given date values.
|
|
274
|
+
* @param {Array<Date | IntlDate>} dates
|
|
275
|
+
* @returns {Date | IntlDate} The smallest date of `dates`
|
|
276
|
+
* @see {@link minDate}
|
|
277
|
+
*/
|
|
278
|
+
static min(...dates) {
|
|
279
|
+
return minDate(...dates);
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Returns the maximum (most distant future) of the given date values.
|
|
283
|
+
* @param {Array<Date | IntlDate>} dates
|
|
284
|
+
* @returns {Date | IntlDate} The largest date of `dates`
|
|
285
|
+
* @see {@link maxDate}
|
|
286
|
+
*/
|
|
287
|
+
static max(...dates) {
|
|
288
|
+
return maxDate(...dates);
|
|
289
|
+
}
|
|
290
|
+
lt(other) {
|
|
291
|
+
return this.isBefore(other);
|
|
292
|
+
}
|
|
293
|
+
eq(other) {
|
|
294
|
+
return this.isSame(other);
|
|
295
|
+
}
|
|
296
|
+
compare(other) {
|
|
297
|
+
if (this.isBefore(other))
|
|
298
|
+
return -1;
|
|
299
|
+
if (this.isAfter(other))
|
|
300
|
+
return 1;
|
|
301
|
+
return 0;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Converts the given date to a UTC timestamp.
|
|
306
|
+
* @param {Date} date
|
|
307
|
+
* @returns {number} The milliseconds between the specified date and the UTC Epoch date.
|
|
308
|
+
*/
|
|
309
|
+
export function dateToUTC(date) {
|
|
310
|
+
return Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds());
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Returns the minimum (most distant past) of the given date values.
|
|
314
|
+
* @param {Array<Date | IntlDate>} dates
|
|
315
|
+
* @returns {Date | IntlDate} The smallest date of `dates`
|
|
316
|
+
*/
|
|
317
|
+
export function minDate(...dates) {
|
|
318
|
+
let result = dates[0];
|
|
319
|
+
for (let i = 1; i < dates.length; i++) {
|
|
320
|
+
if (dates[i].valueOf() < result.valueOf()) {
|
|
321
|
+
result = dates[i];
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return result;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Returns the maximum (most distant future) of the given date values.
|
|
328
|
+
* @param {Array<Date | IntlDate>} dates
|
|
329
|
+
* @returns {Date | IntlDate} The largest date of `dates`
|
|
330
|
+
*/
|
|
331
|
+
export function maxDate(...dates) {
|
|
332
|
+
let result = dates[0];
|
|
333
|
+
for (let i = 1; i < dates.length; i++) {
|
|
334
|
+
if (dates[i].valueOf() > result.valueOf()) {
|
|
335
|
+
result = dates[i];
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return result;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Returns `true` if the year is a leap year, otherwise `false`.
|
|
342
|
+
* @param year The date year
|
|
343
|
+
* @returns {boolean} `true` if is a leap year
|
|
344
|
+
*/
|
|
345
|
+
export function isLeapYear(year) {
|
|
346
|
+
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Get the number of days in the given year.
|
|
350
|
+
* @param year The date year
|
|
351
|
+
* @returns {number} Number of days in year
|
|
352
|
+
*/
|
|
353
|
+
export function daysInYear(year) {
|
|
354
|
+
return isLeapYear(year) ? 366 : 365;
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Get the number of days in the given month and year.
|
|
358
|
+
* @param year The date year
|
|
359
|
+
* @param month The date month
|
|
360
|
+
* @returns {number} Number of days in month
|
|
361
|
+
*/
|
|
362
|
+
export function daysInMonth(year, month) {
|
|
363
|
+
const modMonth = floorMod(month - 1, 12) + 1, modYear = year + (month - modMonth) / 12;
|
|
364
|
+
if (modMonth === 2) {
|
|
365
|
+
return isLeapYear(modYear) ? 29 : 28;
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][modMonth - 1];
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Gets the number of weeks according to locale in the current moment's year.
|
|
373
|
+
* @param {number} weekYear The the date year
|
|
374
|
+
* @returns {number}
|
|
375
|
+
*/
|
|
376
|
+
export function weeksInYear(weekYear) {
|
|
377
|
+
const p1 = (weekYear +
|
|
378
|
+
Math.floor(weekYear / 4) -
|
|
379
|
+
Math.floor(weekYear / 100) +
|
|
380
|
+
Math.floor(weekYear / 400)) %
|
|
381
|
+
7, last = weekYear - 1, p2 = (last + Math.floor(last / 4) - Math.floor(last / 100) + Math.floor(last / 400)) % 7;
|
|
382
|
+
return p1 === 4 || p2 === 3 ? 53 : 52;
|
|
383
|
+
}
|
|
384
|
+
// x % n but takes the sign of n instead of x
|
|
385
|
+
function floorMod(x, n) {
|
|
386
|
+
return x - n * Math.floor(x / n);
|
|
387
|
+
}
|