Skip to content

Commit a0d6fa1

Browse files
committed
fix: improved validating dates from strings
1 parent 780574b commit a0d6fa1

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939

4040
}
4141

42+
/**
43+
* Generates an ObjectId
44+
*/
4245
const ObjectId = (id) => {
4346
// Define the rnd function
4447
const rnd = (r16) => Math.floor(r16).toString(16);
@@ -65,7 +68,11 @@
6568

6669
function isValidDate(value) {
6770
// Check if the value is a string and can be converted to a Date object
68-
if (typeof value === 'string') {
71+
if (typeof value === 'string'
72+
&& !isNaN(value)
73+
&& !(/^[0-9a-fA-F]{24}$/.test(value))
74+
&& !(/^(\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))
75+
) {
6976
const dateObject = new Date(value);
7077

7178
// Check if the result of the Date constructor is a valid Date object

0 commit comments

Comments
 (0)