mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00

* refactor: add and use `parseDate` * style: use proper spelling Co-authored-by: appgurueu <34514239+appgurueu@users.noreply.github.com> --------- Co-authored-by: appgurueu <34514239+appgurueu@users.noreply.github.com>
28 lines
585 B
JavaScript
28 lines
585 B
JavaScript
import { getMonthDays } from './GetMonthDays'
|
|
|
|
function checkDate(date) {
|
|
if (date.day < 1 || date.day > getMonthDays(date.month, date.year)) {
|
|
throw new Error('Invalid day value.')
|
|
}
|
|
}
|
|
|
|
function parseDate(dateString) {
|
|
const regex = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/
|
|
|
|
const match = dateString.match(regex)
|
|
|
|
if (!match) {
|
|
throw new Error("Invalid date format. Please use 'dd/mm/yyyy'.")
|
|
}
|
|
|
|
const res = {
|
|
day: parseInt(match[1], 10),
|
|
month: parseInt(match[2], 10),
|
|
year: parseInt(match[3], 10)
|
|
}
|
|
checkDate(res)
|
|
return res
|
|
}
|
|
|
|
export { parseDate }
|