From 222dfe45d27935225725749063b0fd96701b6db0 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Sun, 22 Aug 2021 14:23:08 +0530 Subject: [PATCH 1/6] Added the Other algorithm category --- Other/DateToDay.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Other/DateToDay.js diff --git a/Other/DateToDay.js b/Other/DateToDay.js new file mode 100644 index 000000000..bec201699 --- /dev/null +++ b/Other/DateToDay.js @@ -0,0 +1,61 @@ +/* + DateToDay Method + ---------------- + 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 pass this date + to the predefined date method and get the date of the day We do + it and find the name of the day, after that give the name of + the day to the user, if the date is wrong, then tell the user + that the given date is not valid. +*/ + +const monthsNameList = { // create a monthsNameList for easy to use. + 1: 'January', + 2: 'February', + 3: 'March', + 4: 'April', + 5: 'May', + 6: 'June', + 7: 'July', + 8: 'August', + 9: 'September', + 10: 'October', + 11: 'November', + 12: 'December' +} + +// show the week day in a number : Sunday - Saturday => 0 - 6 +const daysNameList = { // weeks-day + 0: 'Sunday', + 1: 'Monday', + 2: 'Tuesday', + 3: 'Wednesday', + 4: 'Thursday', + 5: 'Friday', + 6: 'Saturday' +} + +const DateToDay = (date) => { + // firstly, check that input is a string or not. + if (typeof date !== 'string') { + return new TypeError('Argument is not a string.') + } + // extarct the date + const [day, month, year] = date.split('/').map((x) => Number(x)) + // check the data are valid or not. + if (day > 31 || month > 12) { + return new TypeError('Date is not valid.') + } + // create a base date for finding the actuale date. + const baseDate = `${monthsNameList[month]} ${day}, ${year} 23:15:30` + // use the Date class and make an object use of the base date. + const finalDate = new Date(baseDate) + // call a getDay() method of Date() class. + const finalDay = finalDate.getDay() + // return the output. + return daysNameList[finalDay] +} + +module.exports = DateToDay From 560bb77312c98fa505e6710179d8b54c36cb810f Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Mon, 23 Aug 2021 00:55:01 +0530 Subject: [PATCH 2/6] remove DateToDay method --- Other/DateToDay.js | 61 ---------------------------------------------- 1 file changed, 61 deletions(-) delete mode 100644 Other/DateToDay.js diff --git a/Other/DateToDay.js b/Other/DateToDay.js deleted file mode 100644 index bec201699..000000000 --- a/Other/DateToDay.js +++ /dev/null @@ -1,61 +0,0 @@ -/* - DateToDay Method - ---------------- - 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 pass this date - to the predefined date method and get the date of the day We do - it and find the name of the day, after that give the name of - the day to the user, if the date is wrong, then tell the user - that the given date is not valid. -*/ - -const monthsNameList = { // create a monthsNameList for easy to use. - 1: 'January', - 2: 'February', - 3: 'March', - 4: 'April', - 5: 'May', - 6: 'June', - 7: 'July', - 8: 'August', - 9: 'September', - 10: 'October', - 11: 'November', - 12: 'December' -} - -// show the week day in a number : Sunday - Saturday => 0 - 6 -const daysNameList = { // weeks-day - 0: 'Sunday', - 1: 'Monday', - 2: 'Tuesday', - 3: 'Wednesday', - 4: 'Thursday', - 5: 'Friday', - 6: 'Saturday' -} - -const DateToDay = (date) => { - // firstly, check that input is a string or not. - if (typeof date !== 'string') { - return new TypeError('Argument is not a string.') - } - // extarct the date - const [day, month, year] = date.split('/').map((x) => Number(x)) - // check the data are valid or not. - if (day > 31 || month > 12) { - return new TypeError('Date is not valid.') - } - // create a base date for finding the actuale date. - const baseDate = `${monthsNameList[month]} ${day}, ${year} 23:15:30` - // use the Date class and make an object use of the base date. - const finalDate = new Date(baseDate) - // call a getDay() method of Date() class. - const finalDay = finalDate.getDay() - // return the output. - return daysNameList[finalDay] -} - -module.exports = DateToDay From 624c7c3641fed8bed54a3eaf7a0ee6d20dfddc16 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Mon, 23 Aug 2021 01:20:07 +0530 Subject: [PATCH 3/6] change DateToDay method to DateToDayCount method --- Conversions/DateToDayCount.js | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Conversions/DateToDayCount.js diff --git a/Conversions/DateToDayCount.js b/Conversions/DateToDayCount.js new file mode 100644 index 000000000..fa22046d5 --- /dev/null +++ b/Conversions/DateToDayCount.js @@ -0,0 +1,51 @@ +/* +DateToDayCount Method +--------------------- +The DateToDayCount method takes a string date and tells +how many days have passed from that date to the current date. + +Problem source and Explanation : + 1) http://wiki.hotdocs.com/index.php?title=Determine_Number_of_Days_Between_Two_Dates + 2) https://wiki.scn.sap.com/wiki/display/Snippets/calculate+no+of+working+days+between+two+dates +*/ + +const monthsNameList = { // create a monthsNameList for easy to use. + 1: 'January', + 2: 'February', + 3: 'March', + 4: 'April', + 5: 'May', + 6: 'June', + 7: 'July', + 8: 'August', + 9: 'September', + 10: 'October', + 11: 'November', + 12: 'December' +} + +const DateToDayCount = (date) => { + // firstly, check that input is a string or not. + if (typeof date !== 'string') { + return new TypeError('Argument is not a string.') + } + // extarct the date + const [day, month, year] = date.split('/').map((x) => Number(x)) + // check the data are valid or not. + if (day > 31 || month > 12) { + return new TypeError('Date is not valid.') + } + // create a base date for finding the actuale date. + const baseDate = `${monthsNameList[month]} ${day}, ${year} 24:00:00` + // use the Date class and make an object use of the base date. + const inputDate = new Date(baseDate) + // get the current date. + const currentDate = new Date() + // make some celculation for retrive the result. + const diffTime = Math.abs(currentDate - inputDate) + // convert time to day count. + const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)) + return diffDays +} + +module.exports = DateToDayCount From 766d35c2e9566ff70f057c4f7244b2d923607698 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Mon, 23 Aug 2021 12:28:17 +0530 Subject: [PATCH 4/6] add DateToDay method withour Date class --- Conversions/DateToDay.js | 64 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Conversions/DateToDay.js diff --git a/Conversions/DateToDay.js b/Conversions/DateToDay.js new file mode 100644 index 000000000..9d619974e --- /dev/null +++ b/Conversions/DateToDay.js @@ -0,0 +1,64 @@ +/* + DateToDay Method + ---------------- + 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 +*/ + +// 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', + 1: 'Monday', + 2: 'Tuesday', + 3: 'Wednesday', + 4: 'Thursday', + 5: 'Friday', + 6: 'Saturday' +} + +const DateToDay = (date) => { + // firstly, check that input is a string or not. + if (typeof date !== 'string') { + return new TypeError('Argument is not a string.') + } + // extarct the date + const [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]; +} + +// Example : DateToDay("18/12/2020") => Friday + +module.exports = DateToDay From cd26e97514036881610e180c798d47e75f78a355 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Mon, 23 Aug 2021 12:33:05 +0530 Subject: [PATCH 5/6] delete DateToDayCount method --- Conversions/DateToDayCount.js | 51 ----------------------------------- 1 file changed, 51 deletions(-) delete mode 100644 Conversions/DateToDayCount.js diff --git a/Conversions/DateToDayCount.js b/Conversions/DateToDayCount.js deleted file mode 100644 index fa22046d5..000000000 --- a/Conversions/DateToDayCount.js +++ /dev/null @@ -1,51 +0,0 @@ -/* -DateToDayCount Method ---------------------- -The DateToDayCount method takes a string date and tells -how many days have passed from that date to the current date. - -Problem source and Explanation : - 1) http://wiki.hotdocs.com/index.php?title=Determine_Number_of_Days_Between_Two_Dates - 2) https://wiki.scn.sap.com/wiki/display/Snippets/calculate+no+of+working+days+between+two+dates -*/ - -const monthsNameList = { // create a monthsNameList for easy to use. - 1: 'January', - 2: 'February', - 3: 'March', - 4: 'April', - 5: 'May', - 6: 'June', - 7: 'July', - 8: 'August', - 9: 'September', - 10: 'October', - 11: 'November', - 12: 'December' -} - -const DateToDayCount = (date) => { - // firstly, check that input is a string or not. - if (typeof date !== 'string') { - return new TypeError('Argument is not a string.') - } - // extarct the date - const [day, month, year] = date.split('/').map((x) => Number(x)) - // check the data are valid or not. - if (day > 31 || month > 12) { - return new TypeError('Date is not valid.') - } - // create a base date for finding the actuale date. - const baseDate = `${monthsNameList[month]} ${day}, ${year} 24:00:00` - // use the Date class and make an object use of the base date. - const inputDate = new Date(baseDate) - // get the current date. - const currentDate = new Date() - // make some celculation for retrive the result. - const diffTime = Math.abs(currentDate - inputDate) - // convert time to day count. - const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)) - return diffDays -} - -module.exports = DateToDayCount From 63c7732ddae598efbcab4a3f59f0780975db5e70 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Mon, 23 Aug 2021 12:34:17 +0530 Subject: [PATCH 6/6] re-formate wth standard.js --- Conversions/DateToDay.js | 42 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/Conversions/DateToDay.js b/Conversions/DateToDay.js index 9d619974e..ed65d1854 100644 --- a/Conversions/DateToDay.js +++ b/Conversions/DateToDay.js @@ -1,12 +1,12 @@ /* DateToDay Method ---------------- - 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 + 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 @@ -14,18 +14,18 @@ // 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 + 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 @@ -47,16 +47,16 @@ const DateToDay = (date) => { // extarct the date const [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) { + 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) + 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]; + return daysNameList[weekDay] } // Example : DateToDay("18/12/2020") => Friday