Created
January 9, 2025 12:40
-
-
Save pesterhazy/a8505511320dc17a9796ca9a16ccea2e to your computer and use it in GitHub Desktop.
Sad Sunday: The great JavaScript day-of-week confusion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns a normalized weekday int from 0..6 | |
* | |
* Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6 | |
* | |
* This has caused a number of bugs so an explanation is in | |
* order. Javascripts's Date#getDay() methods returns a value from | |
* Sun=0...Sat=6. In other words, the week starts on Sunday. However, | |
* the ISO 8601 week starts on Monday, so the int values range from | |
* Mon=1...Sun=7. The numeric values for Mon...Sat are the same, but | |
* Sunday is represented as 0 in Javascript and as 7 in ISO 8601. | |
* js-joda is using the ISO 8601 standard. | |
* | |
* Here we pick Javascript's representation, so we return Sun=0. | |
*/ | |
export function toJavascriptDayOfWeek(n: number) { | |
return n % 7; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
😂