Calculate week day from UTC date

Thanks to Scott Ventura for reporting this issue! I hope it solves it.
This commit is contained in:
Florine W. Dekker 2021-11-13 11:18:09 +01:00
parent ac74a2f582
commit 8ed98be4a2
Signed by: FWDekker
GPG Key ID: D3DCFAA8A4560BE0
3 changed files with 35 additions and 30 deletions

BIN
package-lock.json generated

Binary file not shown.

View File

@ -1,6 +1,6 @@
{
"name": "doomsday",
"version": "1.3.16",
"version": "1.3.17",
"description": "Test your mastery of Conway's Doomsday rule.",
"author": "F.W. Dekker",
"browser": "dist/bundle.js",
@ -16,15 +16,15 @@
"deploy": "grunt deploy"
},
"devDependencies": {
"grunt": "^1.4.0",
"grunt-cli": "^1.4.2",
"grunt": "^1.4.1",
"grunt-cli": "^1.4.3",
"grunt-contrib-clean": "^2.0.0",
"grunt-contrib-copy": "^1.0.0",
"grunt-contrib-watch": "^1.1.0",
"grunt-focus": "^1.0.0",
"grunt-text-replace": "^0.4.0",
"grunt-webpack": "^4.0.3",
"webpack": "^5.36.0",
"webpack-cli": "^4.6.0"
"grunt-webpack": "^5.0.0",
"webpack": "^5.64.0",
"webpack-cli": "^4.9.1"
}
}

View File

@ -227,7 +227,7 @@ class ToggleableSection {
/**
* A wrapper around the good ol' `Date` class that provides a bunch of useful Doomsday-specific methods.
*
* @property {Date} the underlying date
* @property {Date} date the underlying date
*/
class DoomsdayDate {
/**
@ -256,7 +256,7 @@ class DoomsdayDate {
*/
getCenturyAnchorString() {
const centuryAnchorNumber = (5 * (this.getCentury() % 4)) % 7 + 2;
return DoomsdayDate.dayNumberToString(centuryAnchorNumber);
return DoomsdayDate.getWeekDayOf(centuryAnchorNumber);
};
/**
@ -268,7 +268,7 @@ class DoomsdayDate {
const anchorDate = new Date(this.date);
anchorDate.setDate(4); // 4th
anchorDate.setMonth(3); // April
return DoomsdayDate.dayNumberToString(anchorDate.getDay());
return DoomsdayDate.getWeekDayOf(anchorDate);
};
/**
@ -277,32 +277,36 @@ class DoomsdayDate {
* @return {string} the day of the week of this `DoomsdayDate`
*/
getWeekdayString() {
return DoomsdayDate.dayNumberToString(this.date.getDay());
return DoomsdayDate.getWeekDayOf(this.date);
};
/**
* Returns the name of the day given its 0-based index, where 0 is `Sunday`.
* Returns the week day of [date].
*
* @param dayNumber {number} the number of the day, as returned by `Date`'s `#getDay` function.
* @return {string} the name of the day given its 0-based index, where 0 is `Sunday`
* @param date {Date|number} the date to get the week day of; if it is a `number`, then 0 corresponds to Sunday
* @return {string} the name of the week day corresponding to [date]
*/
static dayNumberToString(dayNumber) {
switch (dayNumber % 7) {
case 0:
return "Sunday";
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
static getWeekDayOf(date) {
if (date instanceof Date) {
return date.toLocaleString("en-US", {timeZone: "GMT", weekday: "long"});
} else {
switch (date % 7) {
case 0:
return "Sunday";
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
}
}
};
@ -341,7 +345,7 @@ class DoomsdayDate {
* @return {DoomsdayDate} a random date
*/
static random() {
// TODO Give custom dates to this method
// TODO Give custom date range to this method
const minDate = new Date("0001-01-01").getTime() / 86400000;
const maxDate = new Date("9999-12-31").getTime() / 86400000;
return new DoomsdayDate(new Date(generateRandom(minDate, maxDate) * 86400000));
@ -475,6 +479,7 @@ doAfterLoad(() => {
quizDate = DoomsdayDate.random();
console.log("# Reset");
console.log(`New date: ${quizDate.date.toISOString().substr(0, 10)}`);
console.log(` ${quizDate.date}`);
console.log(`Century#: ${quizDate.getCentury()}`);
console.log(`Century: ${quizDate.getCenturyAnchorString()}`);
console.log(`Year: ${quizDate.getYearAnchorString()}`);