import { isLeapYear } from '../Maths/LeapYear'
const DateToDay = (dd, mm, yyyy) => {
return (
365 * (yyyy - 1) +
Math.floor((yyyy - 1) / 4) -
Math.floor((yyyy - 1) / 100) +
Math.floor((yyyy - 1) / 400) +
dd +
Math.floor((367 * mm - 362) / 12) +
(mm <= 2 ? 0 : isLeapYear(yyyy) ? -1 : -2)
)
}
const CheckDayAndMonth = (inDay, inMonth) => {
if (inDay <= 0 || inDay > 31 || inMonth <= 0 || inMonth > 12) {
throw new TypeError('Date is not valid.')
}
}
const DateDayDifference = (date1, date2) => {
if (typeof date1 !== 'string' || typeof date2 !== 'string') {
throw new TypeError('Argument is not a string.')
}
const [firstDateDay, firstDateMonth, firstDateYear] = date1
.split('/')
.map((ele) => Number(ele))
const [secondDateDay, secondDateMonth, secondDateYear] = date2
.split('/')
.map((ele) => Number(ele))
CheckDayAndMonth(firstDateDay, firstDateMonth)
CheckDayAndMonth(secondDateDay, secondDateMonth)
return Math.abs(
DateToDay(secondDateDay, secondDateMonth, secondDateYear) -
DateToDay(firstDateDay, firstDateMonth, firstDateYear)
)
}
export { DateDayDifference }