From 0434ac8fd404eea517ed314e7e43907202fde91d Mon Sep 17 00:00:00 2001 From: Steve Bosman Date: Wed, 2 Feb 2022 23:11:27 +0000 Subject: [PATCH] 95 Weekday - move date functions into DateStruct --- 95_Weekday/javascript/weekday.js | 62 ++++++++++++++++---------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/95_Weekday/javascript/weekday.js b/95_Weekday/javascript/weekday.js index ec2386d0..2e1129a0 100644 --- a/95_Weekday/javascript/weekday.js +++ b/95_Weekday/javascript/weekday.js @@ -51,6 +51,34 @@ class DateStruct { this.month = month; this.day = day; } + + /** + * Determine if the date could be a Gregorian date. + * Be aware the Gregorian calendar was not introduced in all places at once, + * see https://en.wikipedia.org/wiki/Gregorian_calendar + * @returns {boolean} true if date could be Gregorian; otherwise false. + */ + isGregorianDate() { + let result = false; + if (this.year > 1582) { + result = true; + } else if (this.year === 1582) { + if (this.month > 10) { + result = true; + } else if (this.month === 10 && this.day >= 15) { + result = true; + } + } + return result; + } + + /** + * Returns a US formatted date, i.e. Month/Day/Year. + * @returns {string} + */ + getFormattedDate() { + return this.month + "/" + this.day + "/" + this.year; + } } class Duration { @@ -88,15 +116,6 @@ async function readDateElements() { return new DateStruct(year, month, day); } -/** - * Returns a US formatted date, i.e. Month/Day/Year. - * @param {DateStruct} date - * @returns {string} - */ -function getFormattedDate(date) { - return date.month + "/" + date.day + "/" + date.year; -} - /** * Calculate years, months and days as factor of days. * This is a naive calculation which assumes all months are 30 days. @@ -257,27 +276,6 @@ function difference(date1, date2) { return new Duration(years, months, days); } -/** - * Determine if the supplied date could be a Gregorian date. - * Be aware the Gregorian calendar was not introduced in all places at once, - * see https://en.wikipedia.org/wiki/Gregorian_calendar - * @param {DateStruct} date - * @returns {boolean} true if date could be Gregorian; otherwise false. - */ -function isGregorianDate(date) { - let result = false; - if (date.year > 1582) { - result = true; - } else if (date.year === 1582) { - if (date.month > 10) { - result = true; - } else if (date.month === 10 && date.day >= 15) { - result = true; - } - } - return result; -} - // Main control section async function main() { print(tab(32) + "WEEKDAY\n"); @@ -296,13 +294,13 @@ async function main() { const dateOfBirth = await readDateElements(); print("\n"); // Test for date before current calendar. - if (!isGregorianDate(dateOfBirth)) { + if (!dateOfBirth.isGregorianDate()) { print("NOT PREPARED TO GIVE DAY OF WEEK PRIOR TO X.XV.MDLXXXII.\n"); } else { const normalisedToday = getNormalisedDay(today); const normalisedDob = getNormalisedDay(dateOfBirth); - const dateOfBirthText = getFormattedDate(dateOfBirth); + const dateOfBirthText = dateOfBirth.getFormattedDate(); let dayOfWeekText = getDayOfWeekText(dateOfBirth); if (normalisedToday < normalisedDob) { print(dateOfBirthText + " WILL BE A " + dayOfWeekText + "\n");