Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: DateToDay #1125

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

Merged
raklaptudirm merged 9 commits into TheAlgorithms:master from chetanppatil:master
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
55 changes: 31 additions & 24 deletions Conversions/DateToDay.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@
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
Algorithm & Explanation : https://en.wikipedia.org/wiki/Zeller%27s_congruence
*/

// 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: 13,
2: 14,
3: 3,
4: 4,
5: 5,
6: 6,
7: 7,
8: 8,
9: 9,
10: 10,
11: 11,
12: 12
}

// 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'
1: 'Sunday',
2: 'Monday',
3: 'Tuesday',
4: 'Wednesday',
5: 'Thursday',
6: 'Friday',
0: 'Saturday'
}

const DateToDay = (date) => {
Expand All @@ -45,18 +45,25 @@ const DateToDay = (date) => {
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) {
if (day < 1 || day > 31 || month > 12 || month < 1) {
return new TypeError('Date is not valid.')
}

// In case of Jan and Feb, we consider it as previous year
// e.g., 1/1/1987 here year is 1986 (-1)
if (month < 3) {
year -= 1
}

// 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((calcMonthList[month] + 1) * 2.6) + yearDigit + (yearDigit / 4) + (century / 4) - (2 * century)) % 7)
// return the weekDay name.
return daysNameList[weekDay]
return daysNameList[Math.floor(weekDay)]
}

// Example : DateToDay("18/12/2020") => Friday
Expand Down
9 changes: 7 additions & 2 deletions Conversions/test/DateToDay.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -16,5 +16,10 @@ 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')
})

test('The date 1/1/2020 is Wednesday', () => {
const res = DateToDay('1/1/2020')
expect(res).toBe('Wednesday')
})

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