Skip to content

Commit 4519b7c

Browse files
committed
feat: isValidDate converts date to system local date
1 parent 33bab33 commit 4519b7c

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/index.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,19 @@
8888
}
8989

9090
function isValidDate(value) {
91-
// Check if the value is a string and can be converted to a Date object
92-
if (typeof value === 'string'
93-
&& !isNaN(value)
94-
&& !(/^[0-9a-fA-F]{24}$/.test(value))
95-
&& !(/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,3})?Z|Mon|Tue|Wed|Thu|Fri|Sat|Sun [A-Za-z]{3} \d{2} \d{4} \d{2}:\d{2}:\d{2} [A-Za-z]{3} \+\d{4}|\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}(\.\d{1,3})?|\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d{1,3})?|\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}(\.\d{1,3})?|Sun|Mon|Tue|Wed|Thu|Fri|Sat),? .+$/.test(value))
96-
) {
97-
const dateObject = new Date(value);
98-
99-
// Check if the result of the Date constructor is a valid Date object
100-
if (!isNaN(dateObject) && dateObject.toString() !== 'Invalid Date') {
101-
return dateObject; // It's a valid Date object
91+
if (typeof value === 'string' && value.length >= 20 && value.length <= 24) {
92+
const iso8601Regex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,3})?Z$/i;
93+
if (iso8601Regex.test(value)) {
94+
const dateObject = new Date(value);
95+
dateObject.setUTCHours(dateObject.getUTCHours(), 0, 0, 0);
96+
if (!isNaN(dateObject)) {
97+
return dateObject; // Is a valid date object and now utc
98+
}
10299
}
103100
}
104101

105-
return value; // It's not a valid Date object
102+
return value; // Is a valid date object adjusted to UTC time
103+
106104
}
107105

108106
function dotNotationToObject(data, obj = {}) {
@@ -424,6 +422,9 @@
424422
queryValue = queryValue.toLowerCase()
425423
}
426424

425+
dataValue = isValidDate(dataValue)
426+
queryValue = isValidDate(queryValue)
427+
427428
switch (query[i].operator) {
428429
case '$includes':
429430
case 'includes':
@@ -443,19 +444,19 @@
443444
queryStatus = true
444445
break;
445446
case '$lt':
446-
if (dataValue > queryValue)
447+
if (dataValue < queryValue)
447448
queryStatus = true
448449
break;
449450
case '$lte':
450-
if (dataValue >= queryValue)
451+
if (dataValue <= queryValue)
451452
queryStatus = true
452453
break;
453454
case '$gt':
454-
if (dataValue < queryValue)
455+
if (dataValue > queryValue)
455456
queryStatus = true
456457
break;
457458
case '$gte':
458-
if (dataValue <= queryValue)
459+
if (dataValue >= queryValue)
459460
queryStatus = true
460461
break;
461462
case '$in':

0 commit comments

Comments
 (0)