Skip to content

correct implementation for Date to Day function #1134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Conversions/DateToDay.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
19 changes: 15 additions & 4 deletions Conversions/test/DateToDay.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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')
})