Skip to content

Commit 116de15

Browse files
committed
Enhance parseDate function to handle time input correctly and update related tests for improved UTC support
1 parent 00ed6c4 commit 116de15

3 files changed

Lines changed: 22 additions & 16 deletions

File tree

src/helpers/parseDate.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ describe("parseDate", () => {
9393
new Date("2000-02-21T00:00:00.000Z")
9494
);
9595
});
96-
97-
// TODO!!!! time still not working with asUTC: true
98-
test.skip("time", async () => {
96+
test("time", async () => {
9997
expect(parseDate("2000-02-02T40:00:00")).toBeUndefined();
10098

10199
expect(parseDate("2000-02-21T01", { asUTC: true })).toStrictEqual(
@@ -106,8 +104,8 @@ describe("parseDate", () => {
106104
new Date("2000-02-21T00:01:00.000Z")
107105
);
108106

109-
expect(parseDate("2000-02-21T00:00:00", { asUTC: true })).toStrictEqual(
110-
new Date("2000-02-21T00:00:00.000Z")
107+
expect(parseDate("2000-02-21T00:00:01", { asUTC: true })).toStrictEqual(
108+
new Date("2000-02-21T00:00:01.000Z")
111109
);
112110
});
113111
});

src/helpers/parseDate.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,21 @@ export const parseDate = (
3232
if (!Number.isFinite(arg)) return;
3333
}
3434

35-
if (isString(arg) && partialDateRegex.test(arg)) {
36-
// Add time to date string because it will be interpreted
37-
// as UTC date instead of local time, and there is no
38-
// circumstance where UTC as default is desired.
39-
arg = `${
40-
arg + "-01-01".slice(arg.length - 4) // fill missing month and day if needed
41-
}T00:00:00`;
35+
if (isString(arg)) {
36+
if (partialDateRegex.test(arg)) {
37+
// Add time to date string because it will be interpreted
38+
// as UTC date instead of local time, and there is no
39+
// circumstance where UTC as default is desired.
40+
arg = `${
41+
arg + "-01-01".slice(arg.length - 4) // fill missing month and day if needed
42+
}T00:00:00`;
43+
}
44+
45+
const [date, time] = arg.split("T");
46+
// 8 as in "HH:MM:SS"
47+
if (time.length < 8) {
48+
arg = `${date}T${time}${"00:00:00".slice(time.length)}`;
49+
}
4250

4351
if (options?.asUTC) {
4452
arg += "Z"; // In this case, force UTC

src/validators/isPastDate.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ describe("isPastDate", () => {
2626
});
2727

2828
it("should return false when date is after or equal to referenceDate", () => {
29-
const referenceDate = new Date("2023-01-01");
30-
expect(isPastDate("2023-06-01", { referenceDate })).toBe(false);
31-
expect(isPastDate("2024-01-01", { referenceDate })).toBe(false);
32-
expect(isPastDate("2023-01-01", { referenceDate })).toBe(false); // same date
29+
const referenceDate = new Date("2023-01-01T00:00:00Z");
30+
expect(isPastDate("2023-06-01T00:00:00Z", { referenceDate })).toBe(false);
31+
expect(isPastDate("2024-01-01T00:00:00Z", { referenceDate })).toBe(false);
32+
expect(isPastDate("2023-01-01T00:00:00Z", { referenceDate })).toBe(false); // same date
3333
});
3434

3535
it("should work with different date formats as referenceDate", () => {

0 commit comments

Comments
 (0)