-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
28 lines (22 loc) · 741 Bytes
/
util.js
File metadata and controls
28 lines (22 loc) · 741 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function convertToDate(dateString) {
// Check if the string matches the format yyyy-mm-dd
const regex = /^\d{4}-\d{2}-\d{2}$/;
if (!regex.test(dateString)) {
throw new Error("Invalid date format");
}
// Check if the date is valid
const dateParts = dateString.split("-");
const year = parseInt(dateParts[0]);
const month = parseInt(dateParts[1]);
const day = parseInt(dateParts[2]);
const date = new Date(year, month - 1, day);
const isValidDate =
date.getFullYear() === year &&
date.getMonth() === month - 1 &&
date.getDate() === day;
if (!isValidDate) {
throw new Error("Invalid date");
}
return date;
}
module.exports = { convertToDate }