Skip to content

Commit 52e88cf

Browse files
author
Chris Galvan
committed
correct implementation for Date to Day function
Implement correct modulus operator in javascript Adjusted the year if month is Jan or Feb, since we are considering March as the starting month (based on the algorith)
1 parent ea7d06a commit 52e88cf

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

Conversions/DateToDay.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,30 @@ const daysNameList = { // weeks-day
3939
6: 'Saturday'
4040
}
4141

42+
// javascript n % m is the remainder operator which is different than modulus operator
43+
// https://stackoverflow.com/questions/4467539/javascript-modulo-gives-a-negative-result-for-negative-numbers
44+
const mod = (n, m) => {
45+
return ((n % m) + m) % m
46+
}
47+
4248
const DateToDay = (date) => {
4349
// firstly, check that input is a string or not.
4450
if (typeof date !== 'string') {
4551
return new TypeError('Argument is not a string.')
4652
}
4753
// extract the date
48-
const [day, month, year] = date.split('/').map((x) => Number(x))
54+
let [day, month, year] = date.split('/').map((x) => Number(x))
4955
// check the data are valid or not.
5056
if (day < 0 || day > 31 || month > 12 || month < 0) {
5157
return new TypeError('Date is not valid.')
5258
}
59+
// adjust year since Jan & Feb are considered from previous year
60+
if (month < 3) year--
5361
// divide year to century and yearDigit value.
54-
const yearDigit = (year % 100)
5562
const century = Math.floor(year / 100)
63+
const yearDigit = (year - century * 100)
5664
// Apply the algorithm shown above
57-
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)
65+
const weekDay = mod(day + Math.floor((2.6 * calcMonthList[month]) - 0.2) - (2 * century) + yearDigit + Math.floor(yearDigit / 4) + Math.floor(century / 4), 7)
5866
// return the weekDay name.
5967
return daysNameList[weekDay]
6068
}

Conversions/test/DateToDay.test.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { DateToDay } from '../DateToDay'
22

3-
test('The date 18/02/2001 is Monday', () => {
3+
test('The date 18/02/2001 is Sunday', () => {
44
const res = DateToDay('18/02/2001')
5-
expect(res).toBe('Monday')
5+
expect(res).toBe('Sunday')
66
})
77

88
test('The date 18/12/2020 is Friday', () => {
@@ -14,7 +14,18 @@ test('The date 12/12/2012 is Wednesday', () => {
1414
const res = DateToDay('12/12/2012')
1515
expect(res).toBe('Wednesday')
1616
})
17-
test('The date 01/01/2001 is Friday', () => {
17+
18+
test('The date 01/01/2001 is Monday', () => {
1819
const res = DateToDay('01/01/2001')
19-
expect(res).toBe('Friday')
20+
expect(res).toBe('Monday')
21+
})
22+
23+
test('The date 01/01/2020 is Wednesday', () => {
24+
const res = DateToDay('01/01/2020')
25+
expect(res).toBe('Wednesday')
26+
})
27+
28+
test('The date 01/01/1900 is Monday', () => {
29+
const res = DateToDay('01/01/1900')
30+
expect(res).toBe('Monday')
2031
})

0 commit comments

Comments
 (0)