diff --git a/Conversions/DateToDay.js b/Conversions/DateToDay.js index 8683bdec4b..4086e4688b 100644 --- a/Conversions/DateToDay.js +++ b/Conversions/DateToDay.js @@ -39,22 +39,30 @@ const daysNameList = { // weeks-day 6: 'Saturday' } +// javascript n % m is the remainder operator which is different than modulus operator +// https://stackoverflow.com/questions/4467539/javascript-modulo-gives-a-negative-result-for-negative-numbers +const mod = (n, m) => { + return ((n % m) + m) % m +} + const DateToDay = (date) => { // 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)) + 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.') } + // adjust year since Jan & Feb are considered from previous year + if (month < 3) year-- // divide year to century and yearDigit value. - const yearDigit = (year % 100) const century = Math.floor(year / 100) + const yearDigit = (year - century * 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 = mod(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] } diff --git a/Conversions/test/DateToDay.test.js b/Conversions/test/DateToDay.test.js index a932dff08c..1b19a6b8a1 100644 --- a/Conversions/test/DateToDay.test.js +++ b/Conversions/test/DateToDay.test.js @@ -1,8 +1,8 @@ import { DateToDay } from '../DateToDay' -test('The date 18/02/2001 is Monday', () => { +test('The date 18/02/2001 is Sunday', () => { const res = DateToDay('18/02/2001') - expect(res).toBe('Monday') + expect(res).toBe('Sunday') }) test('The date 18/12/2020 is Friday', () => { @@ -14,7 +14,18 @@ test('The date 12/12/2012 is Wednesday', () => { const res = DateToDay('12/12/2012') expect(res).toBe('Wednesday') }) -test('The date 01/01/2001 is Friday', () => { + +test('The date 01/01/2001 is Monday', () => { const res = DateToDay('01/01/2001') - expect(res).toBe('Friday') + expect(res).toBe('Monday') +}) + +test('The date 01/01/2020 is Wednesday', () => { + const res = DateToDay('01/01/2020') + expect(res).toBe('Wednesday') +}) + +test('The date 01/01/1900 is Monday', () => { + const res = DateToDay('01/01/1900') + expect(res).toBe('Monday') })

AltStyle によって変換されたページ (->オリジナル) /