From 310ffca193ad6e75255280e29ffc5205359c16b1 Mon Sep 17 00:00:00 2001 From: Sharan Sukesh <77352136+sharansukesh1003@users.noreply.github.com> Date: Wed, 5 Oct 2022 21:16:48 +0530 Subject: [PATCH 01/10] Fixed DateToDay.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change the algorithm to Zeller’s Congruence refer link - (https://www.geeksforgeeks.org/zellers-congruence-find-day-date/) --- Conversions/DateToDay.js | 45 +++++++++++----------------------------- 1 file changed, 12 insertions(+), 33 deletions(-) diff --git a/Conversions/DateToDay.js b/Conversions/DateToDay.js index 8683bdec4b..602687850e 100644 --- a/Conversions/DateToDay.js +++ b/Conversions/DateToDay.js @@ -4,30 +4,11 @@ The DateToDay method takes a date in string format and returns the name of a day. The approach behind this method is very simple, we first take a string date and check - whether their date is valid or not, if the date is valid - then we do this But apply the algorithm shown below. The - algorithm shown below gives us the number of the day and - finally converts it to the name of the day. - - Algorithm & Explanation : https://cs.uwaterloo.ca/~alopez-o/math-faq/node73.html + whether their date is valid or not, if the date is not valid + then we return a type error, else we apply Zeller's congruence. + Algorithm & Explanation : https://www.geeksforgeeks.org/zellers-congruence-find-day-date/ */ -// March is taken as the first month of the year. -const calcMonthList = { - 1: 11, - 2: 12, - 3: 1, - 4: 2, - 5: 3, - 6: 4, - 7: 5, - 8: 6, - 9: 7, - 10: 8, - 11: 9, - 12: 10 -} - // show the week day in a number : Sunday - Saturday => 0 - 6 const daysNameList = { // weeks-day 0: 'Sunday', @@ -38,25 +19,23 @@ const daysNameList = { // weeks-day 5: 'Friday', 6: 'Saturday' } - const DateToDay = (date) => { - // firstly, check that input is a string or not. + // firstly, check that input is a string or not. if (typeof date !== 'string') { return new TypeError('Argument is not a string.') } - // extract the date - const [day, month, year] = date.split('/').map((x) => Number(x)) + // // extract the date + let [day, month, year] = date.split('/').map((x) => Number(x)) // check the data are valid or not. if (day < 0 || day > 31 || month > 12 || month < 0) { return new TypeError('Date is not valid.') } - // divide year to century and yearDigit value. - const yearDigit = (year % 100) - const century = Math.floor(year / 100) - // Apply the algorithm shown above - const weekDay = Math.abs((day + Math.floor((2.6 * calcMonthList[month]) - 0.2) - (2 * century) + yearDigit + Math.floor(yearDigit / 4) + Math.floor(century / 4)) % 7) - // return the weekDay name. - return daysNameList[weekDay] + // date is resolved based on Zeller's congruence. + if (month < 3) { --year; month += 12; } + const century = Math.floor(year / 100); + year %= 100; + const weekDay = (year + Math.floor(year / 4) + Math.floor(century / 4) - 2 * century + Math.floor((26 * (month + 1)) / 10) + day - 1) % 7; + return daysNameList[(weekDay + 7) % 7]; } // Example : DateToDay("18/12/2020") => Friday From 7df28dd79e7b59957565ba279f7097784969cca6 Mon Sep 17 00:00:00 2001 From: Sharan Sukesh <77352136+sharansukesh1003@users.noreply.github.com> Date: Thu, 6 Oct 2022 19:54:08 +0530 Subject: [PATCH 02/10] Update DateToDay.js --- Conversions/DateToDay.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Conversions/DateToDay.js b/Conversions/DateToDay.js index 602687850e..a0320218e2 100644 --- a/Conversions/DateToDay.js +++ b/Conversions/DateToDay.js @@ -24,14 +24,14 @@ const DateToDay = (date) => { if (typeof date !== 'string') { return new TypeError('Argument is not a string.') } - // // extract the date + // extract the date let [day, month, year] = date.split('/').map((x) => Number(x)) // check the data are valid or not. if (day < 0 || day > 31 || month > 12 || month < 0) { return new TypeError('Date is not valid.') } // date is resolved based on Zeller's congruence. - if (month < 3) { --year; month += 12; } + if (month < 3) { year--; month += 12; } const century = Math.floor(year / 100); year %= 100; const weekDay = (year + Math.floor(year / 4) + Math.floor(century / 4) - 2 * century + Math.floor((26 * (month + 1)) / 10) + day - 1) % 7; From 777e45f1c80538d50b0d32dc6f8299099b48283e Mon Sep 17 00:00:00 2001 From: Sharan Sukesh <77352136+sharansukesh1003@users.noreply.github.com> Date: Thu, 6 Oct 2022 20:06:20 +0530 Subject: [PATCH 03/10] Update DateToDay.js --- Conversions/DateToDay.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Conversions/DateToDay.js b/Conversions/DateToDay.js index a0320218e2..e8f1bbbed5 100644 --- a/Conversions/DateToDay.js +++ b/Conversions/DateToDay.js @@ -31,7 +31,7 @@ const DateToDay = (date) => { return new TypeError('Date is not valid.') } // date is resolved based on Zeller's congruence. - if (month < 3) { year--; month += 12; } + if (month < 3) { --year; month += 12; } const century = Math.floor(year / 100); year %= 100; const weekDay = (year + Math.floor(year / 4) + Math.floor(century / 4) - 2 * century + Math.floor((26 * (month + 1)) / 10) + day - 1) % 7; From 7e75b661b6e128eb1e0df905aa13285f14d8f3bc Mon Sep 17 00:00:00 2001 From: Sharan Sukesh <77352136+sharansukesh1003@users.noreply.github.com> Date: Thu, 6 Oct 2022 20:19:33 +0530 Subject: [PATCH 04/10] Invalid test cases The test case '18/02/2001' and '01/01/2001' were Invalid. I have corrected them and also attached the calendar link for reference (https://www.timeanddate.com/calendar/?year=2001&country=35) --- Conversions/test/DateToDay.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Conversions/test/DateToDay.test.js b/Conversions/test/DateToDay.test.js index a932dff08c..d5cec6c4fa 100644 --- a/Conversions/test/DateToDay.test.js +++ b/Conversions/test/DateToDay.test.js @@ -2,7 +2,7 @@ import { DateToDay } from '../DateToDay' test('The date 18/02/2001 is Monday', () => { const res = DateToDay('18/02/2001') - expect(res).toBe('Monday') + expect(res).toBe('Sunday') }) test('The date 18/12/2020 is Friday', () => { @@ -16,5 +16,5 @@ test('The date 12/12/2012 is Wednesday', () => { }) test('The date 01/01/2001 is Friday', () => { const res = DateToDay('01/01/2001') - expect(res).toBe('Friday') + expect(res).toBe('Monday') }) From 88604e49751a2e8ec381b37f3b285b099da52194 Mon Sep 17 00:00:00 2001 From: Sharan Sukesh <77352136+sharansukesh1003@users.noreply.github.com> Date: Thu, 6 Oct 2022 20:37:32 +0530 Subject: [PATCH 05/10] Code Style Fix First time contribution, sorry for the inconvenience. --- Conversions/DateToDay.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Conversions/DateToDay.js b/Conversions/DateToDay.js index e8f1bbbed5..19e10d661c 100644 --- a/Conversions/DateToDay.js +++ b/Conversions/DateToDay.js @@ -20,7 +20,7 @@ const daysNameList = { // weeks-day 6: 'Saturday' } const DateToDay = (date) => { - // firstly, check that input is a string or not. + // firstly, check that input is a string or not. if (typeof date !== 'string') { return new TypeError('Argument is not a string.') } @@ -31,11 +31,11 @@ const DateToDay = (date) => { return new TypeError('Date is not valid.') } // date is resolved based on Zeller's congruence. - if (month < 3) { --year; month += 12; } - const century = Math.floor(year / 100); - year %= 100; - const weekDay = (year + Math.floor(year / 4) + Math.floor(century / 4) - 2 * century + Math.floor((26 * (month + 1)) / 10) + day - 1) % 7; - return daysNameList[(weekDay + 7) % 7]; + if (month < 3) { --year; month += 12 } + const century = Math.floor(year / 100) + year %= 100 + const weekDay = (year + Math.floor(year / 4) + Math.floor(century / 4) - 2 * century + Math.floor((26 * (month + 1)) / 10) + day - 1) % 7 + return daysNameList[(weekDay + 7) % 7] } // Example : DateToDay("18/12/2020") => Friday From f437c62c3b7d55bdfe6a316863e14e5edd60d0a8 Mon Sep 17 00:00:00 2001 From: Sharan Sukesh <77352136+sharansukesh1003@users.noreply.github.com> Date: Fri, 7 Oct 2022 10:51:56 +0530 Subject: [PATCH 06/10] Update DateToDay.js Made year-- --- Conversions/DateToDay.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Conversions/DateToDay.js b/Conversions/DateToDay.js index 19e10d661c..1164a7b2e9 100644 --- a/Conversions/DateToDay.js +++ b/Conversions/DateToDay.js @@ -31,7 +31,7 @@ const DateToDay = (date) => { return new TypeError('Date is not valid.') } // date is resolved based on Zeller's congruence. - if (month < 3) { --year; month += 12 } + if (month < 3) { year--; month += 12 } const century = Math.floor(year / 100) year %= 100 const weekDay = (year + Math.floor(year / 4) + Math.floor(century / 4) - 2 * century + Math.floor((26 * (month + 1)) / 10) + day - 1) % 7 From 73eac53123ef0011aa243f8ca5fbc889f9c66d8e Mon Sep 17 00:00:00 2001 From: Sharan Sukesh <77352136+sharansukesh1003@users.noreply.github.com> Date: Fri, 7 Oct 2022 14:25:08 +0530 Subject: [PATCH 07/10] Comment made more specific Updated the comment and renamed the daysNameList to daysNameDict. If there is any changes to be done please let me know, if not then please accept my PR and merge this. I hope I did a great job as this is my first time exposure to open source. --- Conversions/DateToDay.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Conversions/DateToDay.js b/Conversions/DateToDay.js index 1164a7b2e9..fa635dea13 100644 --- a/Conversions/DateToDay.js +++ b/Conversions/DateToDay.js @@ -10,7 +10,7 @@ */ // show the week day in a number : Sunday - Saturday => 0 - 6 -const daysNameList = { // weeks-day +const daysNameDict = { // weeks-day 0: 'Sunday', 1: 'Monday', 2: 'Tuesday', @@ -30,12 +30,12 @@ const DateToDay = (date) => { if (day < 0 || day > 31 || month > 12 || month < 0) { return new TypeError('Date is not valid.') } - // date is resolved based on Zeller's congruence. + // count Jan & Feb as months 13 & 14 of the previous year if (month < 3) { year--; month += 12 } const century = Math.floor(year / 100) year %= 100 const weekDay = (year + Math.floor(year / 4) + Math.floor(century / 4) - 2 * century + Math.floor((26 * (month + 1)) / 10) + day - 1) % 7 - return daysNameList[(weekDay + 7) % 7] + return daysNameDict[(weekDay + 7) % 7] } // Example : DateToDay("18/12/2020") => Friday From 31c8442ddb8a523786eff92348bfcd0fed732437 Mon Sep 17 00:00:00 2001 From: Sharan Sukesh <77352136+sharansukesh1003@users.noreply.github.com> Date: Fri, 7 Oct 2022 14:44:43 +0530 Subject: [PATCH 08/10] Changed dict to array --- Conversions/DateToDay.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Conversions/DateToDay.js b/Conversions/DateToDay.js index fa635dea13..addc5df805 100644 --- a/Conversions/DateToDay.js +++ b/Conversions/DateToDay.js @@ -9,16 +9,16 @@ Algorithm & Explanation : https://www.geeksforgeeks.org/zellers-congruence-find-day-date/ */ -// show the week day in a number : Sunday - Saturday => 0 - 6 -const daysNameDict = { // weeks-day - 0: 'Sunday', - 1: 'Monday', - 2: 'Tuesday', - 3: 'Wednesday', - 4: 'Thursday', - 5: 'Friday', - 6: 'Saturday' -} +// days in a week : Sunday - Saturday => 0 - 6 +const daysNameList = [ + 'Sunday', + 'Monday', + 'Tuesday', + 'Wednesday', + 'Thursday', + 'Friday', + 'Saturday' +] const DateToDay = (date) => { // firstly, check that input is a string or not. if (typeof date !== 'string') { @@ -35,7 +35,7 @@ const DateToDay = (date) => { const century = Math.floor(year / 100) year %= 100 const weekDay = (year + Math.floor(year / 4) + Math.floor(century / 4) - 2 * century + Math.floor((26 * (month + 1)) / 10) + day - 1) % 7 - return daysNameDict[(weekDay + 7) % 7] + return daysNameList[(weekDay + 7) % 7] } // Example : DateToDay("18/12/2020") => Friday From ca3555ce9d83f8fe38cd91b797d3b3ea712971ac Mon Sep 17 00:00:00 2001 From: Sharan Sukesh <77352136+sharansukesh1003@users.noreply.github.com> Date: Fri, 7 Oct 2022 14:57:29 +0530 Subject: [PATCH 09/10] Changed List to Array Changed List to Array and also fixed to trailing spaces. --- Conversions/DateToDay.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Conversions/DateToDay.js b/Conversions/DateToDay.js index addc5df805..8759982235 100644 --- a/Conversions/DateToDay.js +++ b/Conversions/DateToDay.js @@ -10,7 +10,7 @@ */ // days in a week : Sunday - Saturday => 0 - 6 -const daysNameList = [ +const daysNameList = [ 'Sunday', 'Monday', 'Tuesday', From be5f0071236cbb592bbb27f7e3711639de5d3645 Mon Sep 17 00:00:00 2001 From: Sharan Sukesh <77352136+sharansukesh1003@users.noreply.github.com> Date: Sat, 8 Oct 2022 22:05:57 +0530 Subject: [PATCH 10/10] Fix DateToDay.js removed %7 from line 36 --- Conversions/DateToDay.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Conversions/DateToDay.js b/Conversions/DateToDay.js index 8759982235..d295ff25a9 100644 --- a/Conversions/DateToDay.js +++ b/Conversions/DateToDay.js @@ -34,7 +34,7 @@ const DateToDay = (date) => { if (month < 3) { year--; month += 12 } const century = Math.floor(year / 100) year %= 100 - const weekDay = (year + Math.floor(year / 4) + Math.floor(century / 4) - 2 * century + Math.floor((26 * (month + 1)) / 10) + day - 1) % 7 + const weekDay = (year + Math.floor(year / 4) + Math.floor(century / 4) - 2 * century + Math.floor((26 * (month + 1)) / 10) + day - 1) return daysNameList[(weekDay + 7) % 7] }