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 #1126

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
sharansukesh1003 wants to merge 10 commits into TheAlgorithms:master from sharansukesh1003:master
Closed
Show file tree
Hide file tree
Changes from 6 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
39 changes: 9 additions & 30 deletions Conversions/DateToDay.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,11 @@
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
whether their date is valid or not, if the date is not valid
then we return a type error, else we apply Zeller's congruence.
Algorithm & Explanation : https://www.geeksforgeeks.org/zellers-congruence-find-day-date/
*/

// 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
Copy link
Collaborator

@appgurueu appgurueu Oct 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use an array here.

0: 'Sunday',
Expand All @@ -38,25 +19,23 @@ const daysNameList = { // weeks-day
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.')
}
// 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.')
}
// divide year to century and yearDigit value.
const yearDigit = (year % 100)
// date is resolved based on Zeller's congruence.
Copy link
Collaborator

@appgurueu appgurueu Oct 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the overarching comment. This should be specific to the following line: "Count Jan & Feb as months 13 & 14 of the previous year".

Copy link
Author

@sharansukesh1003 sharansukesh1003 Oct 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh great input, I will change it.

if (month < 3) { year--; month += 12 }
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]
year %= 100
const weekDay = (year + Math.floor(year / 4) + Math.floor(century / 4) - 2 * century + Math.floor((26 * (month + 1)) / 10) + day - 1) % 7
Copy link
Collaborator

@appgurueu appgurueu Oct 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the % 7 here if you apply % 7 below.

Copy link
Author

@sharansukesh1003 sharansukesh1003 Oct 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm understood, changed it.

return daysNameList[(weekDay + 7) % 7]
}

// Example : DateToDay("18/12/2020") => Friday
Expand Down
4 changes: 2 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,5 @@ 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')
})

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