refactor: add and use parseDate (#1643)

* 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>
This commit is contained in:
Piotr Idzik
2024-04-03 17:19:26 +02:00
committed by GitHub
parent 34a663aca7
commit 9c622dd089
4 changed files with 78 additions and 32 deletions

View File

@@ -7,6 +7,7 @@
*/
import { isLeapYear } from '../Maths/LeapYear'
import { parseDate } from '../Timing-Functions/ParseDate'
const DateToDay = (dd, mm, yyyy) => {
return (
@@ -20,32 +21,13 @@ const DateToDay = (dd, mm, yyyy) => {
)
}
const CheckDayAndMonth = (inDay, inMonth) => {
if (inDay <= 0 || inDay > 31 || inMonth <= 0 || inMonth > 12) {
throw new TypeError('Date is not valid.')
}
}
const DateDayDifference = (date1, date2) => {
// firstly, check that both input are string or not.
if (typeof date1 !== 'string' || typeof date2 !== 'string') {
throw new TypeError('Argument is not a string.')
}
// extract the first date
const [firstDateDay, firstDateMonth, firstDateYear] = date1
.split('/')
.map((ele) => Number(ele))
// extract the second date
const [secondDateDay, secondDateMonth, secondDateYear] = date2
.split('/')
.map((ele) => Number(ele))
// check the both data are valid or not.
CheckDayAndMonth(firstDateDay, firstDateMonth)
CheckDayAndMonth(secondDateDay, secondDateMonth)
const firstDate = parseDate(date1)
const secondDate = parseDate(date2)
return Math.abs(
DateToDay(secondDateDay, secondDateMonth, secondDateYear) -
DateToDay(firstDateDay, firstDateMonth, firstDateYear)
DateToDay(secondDate.day, secondDate.month, secondDate.year) -
DateToDay(firstDate.day, firstDate.month, firstDate.year)
)
}

View File

@@ -12,6 +12,8 @@
Algorithm & Explanation : https://en.wikipedia.org/wiki/Zeller%27s_congruence
*/
import { parseDate } from '../Timing-Functions/ParseDate'
// Array holding name of the day: Saturday - Sunday - Friday => 0 - 1 - 6
const daysNameArr = [
'Saturday',
@@ -25,15 +27,10 @@ const daysNameArr = [
const DateToDay = (date) => {
// firstly, check that input is a string or not.
if (typeof date !== 'string') {
throw new TypeError('Argument is not a string.')
}
// extract the date
let [day, month, year] = date.split('/').map((x) => Number(x))
// check the data are valid or not.
if (day < 1 || day > 31 || month > 12 || month < 1) {
throw new TypeError('Date is not valid.')
}
const dateStruct = parseDate(date)
let year = dateStruct.year
let month = dateStruct.month
let day = dateStruct.day
// In case of Jan and Feb:
// Year: we consider it as previous year