nhb-toolbox 3.6.45 → 3.7.5
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/dist/date/Chronos.d.ts +68 -3
- package/dist/date/Chronos.d.ts.map +1 -1
- package/dist/date/Chronos.js +212 -7
- package/package.json +1 -1
package/dist/date/Chronos.d.ts
CHANGED
|
@@ -29,6 +29,8 @@ export declare class Chronos {
|
|
|
29
29
|
toDate(): Date;
|
|
30
30
|
/** * Returns a string representation of a date. The format of the string depends on the locale. */
|
|
31
31
|
toString(): string;
|
|
32
|
+
/** * Returns ISO string with local time zone offset */
|
|
33
|
+
toLocalISOString(): string;
|
|
32
34
|
/**
|
|
33
35
|
* * Wrapper over native `toLocaleString`
|
|
34
36
|
* @description Converts a date and time to a string by using the current or specified locale.
|
|
@@ -41,10 +43,18 @@ export declare class Chronos {
|
|
|
41
43
|
toISOString(): string;
|
|
42
44
|
/** * Returns the time value in milliseconds since midnight, January 1, 1970 UTC. */
|
|
43
45
|
getTimeStamp(): number;
|
|
44
|
-
/** * Returns the date value as a `Date` object. */
|
|
45
|
-
get date(): Date;
|
|
46
46
|
/** * Returns the time value in milliseconds since midnight, January 1, 1970 UTC. */
|
|
47
47
|
get unix(): number;
|
|
48
|
+
get year(): number;
|
|
49
|
+
get month(): number;
|
|
50
|
+
get date(): number;
|
|
51
|
+
get day(): number;
|
|
52
|
+
get hour(): number;
|
|
53
|
+
get minute(): number;
|
|
54
|
+
get second(): number;
|
|
55
|
+
get millisecond(): number;
|
|
56
|
+
/** * ISO weekday: 1 = Monday, 7 = Sunday */
|
|
57
|
+
get isoWeekday(): number;
|
|
48
58
|
/**
|
|
49
59
|
* @instance Returns the current date and time in a specified format in local time.
|
|
50
60
|
* * Default format is dd, `MMM DD, YYYY HH:mm:ss` = `Sun, Apr 06, 2025 16:11:55:379`
|
|
@@ -210,7 +220,7 @@ export declare class Chronos {
|
|
|
210
220
|
* * Returns a new Chronos instance at the start of a given unit.
|
|
211
221
|
* @param unit The unit to reset (e.g., year, month, day).
|
|
212
222
|
*/
|
|
213
|
-
startOf(unit: TimeUnit): Chronos;
|
|
223
|
+
startOf(unit: TimeUnit | 'week'): Chronos;
|
|
214
224
|
/**
|
|
215
225
|
* * Returns a new Chronos instance at the end of a given unit.
|
|
216
226
|
* @param unit The unit to adjust (e.g., year, month, day).
|
|
@@ -264,5 +274,60 @@ export declare class Chronos {
|
|
|
264
274
|
calendar(baseDate?: number | string | Date | Chronos): string;
|
|
265
275
|
/** * Returns a short human-readable string like "2h ago", "in 5m" */
|
|
266
276
|
fromNowShort(): string;
|
|
277
|
+
/** * Returns ISO week number */
|
|
278
|
+
getWeek(): number;
|
|
279
|
+
/** * Returns ISO week year */
|
|
280
|
+
getWeekYear(): number;
|
|
281
|
+
/** * Returns day of year (1 - 366) */
|
|
282
|
+
getDayOfYear(): number;
|
|
283
|
+
/**
|
|
284
|
+
* * Checks if the current date is between the given start and end dates.
|
|
285
|
+
*
|
|
286
|
+
* @param start - The start of the range.
|
|
287
|
+
* @param end - The end of the range.
|
|
288
|
+
* @param inclusive - Specifies whether the comparison is inclusive or exclusive:
|
|
289
|
+
* - `'[]'`: inclusive of both start and end (≥ start and ≤ end)
|
|
290
|
+
* - `'[)'`: inclusive of start, exclusive of end (≥ start and < end)
|
|
291
|
+
* - `'(]'`: exclusive of start, inclusive of end (> start and ≤ end)
|
|
292
|
+
* - `'()'`: exclusive of both start and end (> start and < end)
|
|
293
|
+
*
|
|
294
|
+
* @returns `true` if the current date is within the specified range based on the `inclusive` mode.
|
|
295
|
+
*/
|
|
296
|
+
isBetween(start: number | string | Date | Chronos, end: number | string | Date | Chronos, inclusive?: '[]' | '[)' | '(]' | '()'): boolean;
|
|
297
|
+
/** * Returns number of days in current month */
|
|
298
|
+
daysInMonth(): number;
|
|
299
|
+
/** * Converts to object with all date unit parts */
|
|
300
|
+
toObject(): {
|
|
301
|
+
year: number;
|
|
302
|
+
month: number;
|
|
303
|
+
isoMonth: number;
|
|
304
|
+
date: number;
|
|
305
|
+
day: number;
|
|
306
|
+
isoDay: number;
|
|
307
|
+
hour: number;
|
|
308
|
+
minute: number;
|
|
309
|
+
second: number;
|
|
310
|
+
millisecond: number;
|
|
311
|
+
};
|
|
312
|
+
/** * Converts to array with all date unit parts */
|
|
313
|
+
toArray(): [number, number, number, number, number, number, number];
|
|
314
|
+
/** * Returns offset like +06:00 */
|
|
315
|
+
getUTCOffset(): string;
|
|
316
|
+
/** * Checks if currently in DST */
|
|
317
|
+
isDST(): boolean;
|
|
318
|
+
/** * Returns new Chronos instance in UTC */
|
|
319
|
+
/** * Returns new Chronos instance in local time */
|
|
320
|
+
/** @static Parses a date string with a given format (partial support) */
|
|
321
|
+
static parse(dateStr: string, format: string): Chronos;
|
|
322
|
+
/**
|
|
323
|
+
* @static Returns earliest Chronos
|
|
324
|
+
* @param dates Date inputs.
|
|
325
|
+
*/
|
|
326
|
+
static min(...dates: (number | string | Date | Chronos)[]): Chronos;
|
|
327
|
+
/**
|
|
328
|
+
* @static Returns latest Chronos
|
|
329
|
+
* @param dates Date inputs.
|
|
330
|
+
*/
|
|
331
|
+
static max(...dates: (number | string | Date | Chronos)[]): Chronos;
|
|
267
332
|
}
|
|
268
333
|
//# sourceMappingURL=Chronos.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Chronos.d.ts","sourceRoot":"","sources":["../../src/date/Chronos.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAGlD,OAAO,KAAK,EAEX,aAAa,EACb,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,MAAM,SAAS,CAAC;AAGjB,qBAAa,OAAO;;IAInB;;;;;;;;OAQG;gBACS,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO;IAOpD,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,MAAM,CAEjC;IAED;;;;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"}
|
package/dist/date/Chronos.js
CHANGED
|
@@ -19,7 +19,7 @@ export class Chronos {
|
|
|
19
19
|
// this.preview = this.toISOString();
|
|
20
20
|
}
|
|
21
21
|
get [Symbol.toStringTag]() {
|
|
22
|
-
return this.
|
|
22
|
+
return this.toLocalISOString();
|
|
23
23
|
}
|
|
24
24
|
/**
|
|
25
25
|
* * Enables primitive coercion like `console.log`, `${chronos}`, etc.
|
|
@@ -29,7 +29,7 @@ export class Chronos {
|
|
|
29
29
|
[Symbol.toPrimitive](hint) {
|
|
30
30
|
if (hint === 'number')
|
|
31
31
|
return this.valueOf();
|
|
32
|
-
return this.
|
|
32
|
+
return this.toLocalISOString();
|
|
33
33
|
}
|
|
34
34
|
/** * Clones and returns a new Chronos instance with the same date. */
|
|
35
35
|
clone() {
|
|
@@ -37,7 +37,7 @@ export class Chronos {
|
|
|
37
37
|
}
|
|
38
38
|
/** * Enables JSON.stringify and console logging to show readable output. */
|
|
39
39
|
toJSON() {
|
|
40
|
-
return this.
|
|
40
|
+
return this.toLocalISOString();
|
|
41
41
|
}
|
|
42
42
|
/** * Enables arithmetic and comparison operations (e.g., +new Chronos()). */
|
|
43
43
|
valueOf() {
|
|
@@ -51,6 +51,11 @@ export class Chronos {
|
|
|
51
51
|
toString() {
|
|
52
52
|
return this.#date.toString();
|
|
53
53
|
}
|
|
54
|
+
/** * Returns ISO string with local time zone offset */
|
|
55
|
+
toLocalISOString() {
|
|
56
|
+
const pad = (n, p = 2) => String(n).padStart(p, '0');
|
|
57
|
+
return `${this.year}-${pad(this.month + 1)}-${pad(this.date)}T${pad(this.hour)}:${pad(this.minute)}:${pad(this.second)}.${pad(this.millisecond, 3)}${this.getUTCOffset()}`;
|
|
58
|
+
}
|
|
54
59
|
/**
|
|
55
60
|
* * Wrapper over native `toLocaleString`
|
|
56
61
|
* @description Converts a date and time to a string by using the current or specified locale.
|
|
@@ -69,14 +74,39 @@ export class Chronos {
|
|
|
69
74
|
getTimeStamp() {
|
|
70
75
|
return this.#date.getTime();
|
|
71
76
|
}
|
|
72
|
-
/** * Returns the date value as a `Date` object. */
|
|
73
|
-
get date() {
|
|
74
|
-
return this.#date;
|
|
75
|
-
}
|
|
76
77
|
/** * Returns the time value in milliseconds since midnight, January 1, 1970 UTC. */
|
|
77
78
|
get unix() {
|
|
78
79
|
return this.#date.getTime();
|
|
79
80
|
}
|
|
81
|
+
get year() {
|
|
82
|
+
return this.#date.getFullYear();
|
|
83
|
+
}
|
|
84
|
+
get month() {
|
|
85
|
+
return this.#date.getMonth();
|
|
86
|
+
}
|
|
87
|
+
get date() {
|
|
88
|
+
return this.#date.getDate();
|
|
89
|
+
}
|
|
90
|
+
get day() {
|
|
91
|
+
return this.#date.getDay();
|
|
92
|
+
}
|
|
93
|
+
get hour() {
|
|
94
|
+
return this.#date.getHours();
|
|
95
|
+
}
|
|
96
|
+
get minute() {
|
|
97
|
+
return this.#date.getMinutes();
|
|
98
|
+
}
|
|
99
|
+
get second() {
|
|
100
|
+
return this.#date.getSeconds();
|
|
101
|
+
}
|
|
102
|
+
get millisecond() {
|
|
103
|
+
return this.#date.getMilliseconds();
|
|
104
|
+
}
|
|
105
|
+
/** * ISO weekday: 1 = Monday, 7 = Sunday */
|
|
106
|
+
get isoWeekday() {
|
|
107
|
+
const day = this.day;
|
|
108
|
+
return day === 0 ? 7 : day;
|
|
109
|
+
}
|
|
80
110
|
/**
|
|
81
111
|
* @instance Returns the current date and time in a specified format in local time.
|
|
82
112
|
* * Default format is dd, `MMM DD, YYYY HH:mm:ss` = `Sun, Apr 06, 2025 16:11:55:379`
|
|
@@ -529,6 +559,13 @@ export class Chronos {
|
|
|
529
559
|
d.setDate(1);
|
|
530
560
|
d.setHours(0, 0, 0, 0);
|
|
531
561
|
break;
|
|
562
|
+
case 'week': {
|
|
563
|
+
const day = d.getDay(); // 0 (Sun) - 6 (Sat)
|
|
564
|
+
const diff = (day + 6) % 7; // convert Sunday=0 to 6, Monday=1 to 0, etc.
|
|
565
|
+
d.setDate(d.getDate() - diff);
|
|
566
|
+
d.setHours(0, 0, 0, 0);
|
|
567
|
+
break;
|
|
568
|
+
}
|
|
532
569
|
case 'day':
|
|
533
570
|
d.setHours(0, 0, 0, 0);
|
|
534
571
|
break;
|
|
@@ -749,4 +786,172 @@ export class Chronos {
|
|
|
749
786
|
return `${suffix}${Math.floor(abs / 31536000)}y${postfix}`;
|
|
750
787
|
}
|
|
751
788
|
}
|
|
789
|
+
/** * Returns ISO week number */
|
|
790
|
+
getWeek() {
|
|
791
|
+
// ISO week starts on Monday
|
|
792
|
+
const target = this.startOf('week').add(3, 'day');
|
|
793
|
+
const firstThursday = new Chronos(new Date(target.year, 0, 4))
|
|
794
|
+
.startOf('week')
|
|
795
|
+
.add(3, 'day');
|
|
796
|
+
const daysDiff = target.diff(firstThursday, 'day');
|
|
797
|
+
const week = Math.floor(daysDiff / 7) + 1;
|
|
798
|
+
return week;
|
|
799
|
+
}
|
|
800
|
+
/** * Returns ISO week year */
|
|
801
|
+
getWeekYear() {
|
|
802
|
+
const d = this.startOf('week').add(3, 'day'); // Thursday of current ISO week
|
|
803
|
+
return d.year;
|
|
804
|
+
}
|
|
805
|
+
/** * Returns day of year (1 - 366) */
|
|
806
|
+
getDayOfYear() {
|
|
807
|
+
const start = new Date(this.year, 0, 1);
|
|
808
|
+
const diff = this.#date.getTime() - start.getTime();
|
|
809
|
+
return Math.floor(diff / 86400000) + 1;
|
|
810
|
+
}
|
|
811
|
+
/**
|
|
812
|
+
* * Checks if the current date is between the given start and end dates.
|
|
813
|
+
*
|
|
814
|
+
* @param start - The start of the range.
|
|
815
|
+
* @param end - The end of the range.
|
|
816
|
+
* @param inclusive - Specifies whether the comparison is inclusive or exclusive:
|
|
817
|
+
* - `'[]'`: inclusive of both start and end (≥ start and ≤ end)
|
|
818
|
+
* - `'[)'`: inclusive of start, exclusive of end (≥ start and < end)
|
|
819
|
+
* - `'(]'`: exclusive of start, inclusive of end (> start and ≤ end)
|
|
820
|
+
* - `'()'`: exclusive of both start and end (> start and < end)
|
|
821
|
+
*
|
|
822
|
+
* @returns `true` if the current date is within the specified range based on the `inclusive` mode.
|
|
823
|
+
*/
|
|
824
|
+
isBetween(start, end, inclusive = '()') {
|
|
825
|
+
const s = new Chronos(start).valueOf();
|
|
826
|
+
const e = new Chronos(end).valueOf();
|
|
827
|
+
const t = this.valueOf();
|
|
828
|
+
switch (inclusive) {
|
|
829
|
+
case '[]':
|
|
830
|
+
return t >= s && t <= e;
|
|
831
|
+
case '[)':
|
|
832
|
+
return t >= s && t < e;
|
|
833
|
+
case '(]':
|
|
834
|
+
return t > s && t <= e;
|
|
835
|
+
case '()':
|
|
836
|
+
return t > s && t < e;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
/** * Returns number of days in current month */
|
|
840
|
+
daysInMonth() {
|
|
841
|
+
return new Date(this.year, this.month + 1, 0).getDate();
|
|
842
|
+
}
|
|
843
|
+
/** * Converts to object with all date unit parts */
|
|
844
|
+
toObject() {
|
|
845
|
+
return {
|
|
846
|
+
year: this.year,
|
|
847
|
+
month: this.month,
|
|
848
|
+
isoMonth: this.month + 1,
|
|
849
|
+
date: this.date,
|
|
850
|
+
day: this.day,
|
|
851
|
+
isoDay: this.day + 1,
|
|
852
|
+
hour: this.hour,
|
|
853
|
+
minute: this.minute,
|
|
854
|
+
second: this.second,
|
|
855
|
+
millisecond: this.millisecond,
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
/** * Converts to array with all date unit parts */
|
|
859
|
+
toArray() {
|
|
860
|
+
return [
|
|
861
|
+
this.year,
|
|
862
|
+
this.month,
|
|
863
|
+
this.date,
|
|
864
|
+
this.hour,
|
|
865
|
+
this.minute,
|
|
866
|
+
this.second,
|
|
867
|
+
this.millisecond,
|
|
868
|
+
];
|
|
869
|
+
}
|
|
870
|
+
/** * Returns offset like +06:00 */
|
|
871
|
+
getUTCOffset() {
|
|
872
|
+
const offset = -this.#date.getTimezoneOffset();
|
|
873
|
+
const sign = offset >= 0 ? '+' : '-';
|
|
874
|
+
const pad = (n) => String(Math.floor(Math.abs(n))).padStart(2, '0');
|
|
875
|
+
return `${sign}${pad(offset / 60)}:${pad(offset % 60)}`;
|
|
876
|
+
}
|
|
877
|
+
/** * Checks if currently in DST */
|
|
878
|
+
isDST() {
|
|
879
|
+
const jan = new Date(this.year, 0, 1);
|
|
880
|
+
const jul = new Date(this.year, 6, 1);
|
|
881
|
+
return (Math.min(jan.getTimezoneOffset(), jul.getTimezoneOffset()) !==
|
|
882
|
+
this.#date.getTimezoneOffset());
|
|
883
|
+
}
|
|
884
|
+
/** * Returns new Chronos instance in UTC */
|
|
885
|
+
// toUTC(): Chronos {
|
|
886
|
+
// // const utcDate = new Date(
|
|
887
|
+
// // Date.UTC(
|
|
888
|
+
// // this.#date.getFullYear(),
|
|
889
|
+
// // this.#date.getMonth(),
|
|
890
|
+
// // this.#date.getDate(),
|
|
891
|
+
// // this.#date.getHours(),
|
|
892
|
+
// // this.#date.getMinutes(),
|
|
893
|
+
// // this.#date.getSeconds(),
|
|
894
|
+
// // this.#date.getMilliseconds(),
|
|
895
|
+
// // ),
|
|
896
|
+
// // );
|
|
897
|
+
// // return new Chronos(utcDate);
|
|
898
|
+
// const date = this.#date;
|
|
899
|
+
// const utc = new Date(date.getTime() + date.getTimezoneOffset() * 60000);
|
|
900
|
+
// return new Chronos(utc);
|
|
901
|
+
// }
|
|
902
|
+
/** * Returns new Chronos instance in local time */
|
|
903
|
+
// toLocal(): Chronos {
|
|
904
|
+
// const date = new Date(this.#date.getTime());
|
|
905
|
+
// date.setMinutes(date.getMinutes() + date.getTimezoneOffset());
|
|
906
|
+
// return new Chronos(date);
|
|
907
|
+
// }
|
|
908
|
+
/** @static Parses a date string with a given format (partial support) */
|
|
909
|
+
static parse(dateStr, format) {
|
|
910
|
+
const formatMap = {
|
|
911
|
+
YYYY: 'year',
|
|
912
|
+
MM: 'month',
|
|
913
|
+
DD: 'date',
|
|
914
|
+
HH: 'hour',
|
|
915
|
+
mm: 'minute',
|
|
916
|
+
ss: 'second',
|
|
917
|
+
};
|
|
918
|
+
const regex = format.replace(/YYYY|MM|DD|HH|mm|ss/g, (match) => {
|
|
919
|
+
return `(?<${match}>\\d{${match.length}})`;
|
|
920
|
+
});
|
|
921
|
+
const match = new RegExp(`^${regex}$`).exec(dateStr);
|
|
922
|
+
if (!match?.groups) {
|
|
923
|
+
throw new Error('Invalid date format');
|
|
924
|
+
}
|
|
925
|
+
const values = Object.entries(match.groups).reduce((acc, [key, val]) => {
|
|
926
|
+
const map = formatMap[key];
|
|
927
|
+
if (map) {
|
|
928
|
+
acc[map] = Number(val);
|
|
929
|
+
}
|
|
930
|
+
return acc;
|
|
931
|
+
}, {});
|
|
932
|
+
return new Chronos(new Date(values.year ?? 1970, (values.month ?? 1) - 1, values.date ?? 1, values.hour ?? 0, values.minute ?? 0, values.second ?? 0));
|
|
933
|
+
}
|
|
934
|
+
// /**
|
|
935
|
+
// * @static Creates UTC Chronos
|
|
936
|
+
// * @param dateLike Date input to create utc time.
|
|
937
|
+
// */
|
|
938
|
+
// static utc(dateLike: number | string | Date | Chronos): Chronos {
|
|
939
|
+
// const date = new Chronos(dateLike).toDate();
|
|
940
|
+
// const utc = new Date(date.getTime() + date.getTimezoneOffset() * 60000);
|
|
941
|
+
// return new Chronos(utc);
|
|
942
|
+
// }
|
|
943
|
+
/**
|
|
944
|
+
* @static Returns earliest Chronos
|
|
945
|
+
* @param dates Date inputs.
|
|
946
|
+
*/
|
|
947
|
+
static min(...dates) {
|
|
948
|
+
return new Chronos(Math.min(...dates.map((d) => new Chronos(d).valueOf())));
|
|
949
|
+
}
|
|
950
|
+
/**
|
|
951
|
+
* @static Returns latest Chronos
|
|
952
|
+
* @param dates Date inputs.
|
|
953
|
+
*/
|
|
954
|
+
static max(...dates) {
|
|
955
|
+
return new Chronos(Math.max(...dates.map((d) => new Chronos(d).valueOf())));
|
|
956
|
+
}
|
|
752
957
|
}
|
package/package.json
CHANGED