Use Luxon to manipulate dates
This commit is contained in:
parent
d443fb7c48
commit
9b2a1600d5
Binary file not shown.
|
@ -15,7 +15,11 @@
|
||||||
"dev:server": "grunt dev:server",
|
"dev:server": "grunt dev:server",
|
||||||
"deploy": "grunt deploy"
|
"deploy": "grunt deploy"
|
||||||
},
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"luxon": "^2.3.1"
|
||||||
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/luxon": "^2.3.0",
|
||||||
"grunt": "^1.4.1",
|
"grunt": "^1.4.1",
|
||||||
"grunt-cli": "^1.4.3",
|
"grunt-cli": "^1.4.3",
|
||||||
"grunt-contrib-clean": "^2.0.0",
|
"grunt-contrib-clean": "^2.0.0",
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const {$, doAfterLoad, footer, header, nav} = window.fwdekker;
|
const {$, doAfterLoad, footer, header, nav} = window.fwdekker;
|
||||||
|
|
||||||
|
import {DateTime} from "luxon";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a number between `min` (inclusive) and `max` (inclusive).
|
* Returns a number between `min` (inclusive) and `max` (inclusive).
|
||||||
|
@ -255,7 +257,7 @@ class DoomsdayDate {
|
||||||
/**
|
/**
|
||||||
* The underlying date.
|
* The underlying date.
|
||||||
*/
|
*/
|
||||||
readonly date: Date;
|
readonly date: DateTime;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -263,7 +265,7 @@ class DoomsdayDate {
|
||||||
*
|
*
|
||||||
* @param date the date to be wrapped
|
* @param date the date to be wrapped
|
||||||
*/
|
*/
|
||||||
constructor(date: Date) {
|
constructor(date: DateTime) {
|
||||||
this.date = date;
|
this.date = date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -274,7 +276,7 @@ class DoomsdayDate {
|
||||||
* @return the number of this `DoomsdayDate`'s century
|
* @return the number of this `DoomsdayDate`'s century
|
||||||
*/
|
*/
|
||||||
getCentury(): number {
|
getCentury(): number {
|
||||||
return Math.floor(this.date.getFullYear() / 100);
|
return Math.floor(this.date.year / 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -283,8 +285,7 @@ class DoomsdayDate {
|
||||||
* @return the day of the week of the anchor of this `DoomsdayDate`'s century
|
* @return the day of the week of the anchor of this `DoomsdayDate`'s century
|
||||||
*/
|
*/
|
||||||
getCenturyAnchorString(): string {
|
getCenturyAnchorString(): string {
|
||||||
const centuryAnchorNumber = (5 * (this.getCentury() % 4)) % 7 + 2;
|
return this.date.set({year: this.getCentury() * 100, month: 4, day: 4}).setLocale("en-US").weekdayLong;
|
||||||
return DoomsdayDate.getWeekDayOf(centuryAnchorNumber);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -293,11 +294,7 @@ class DoomsdayDate {
|
||||||
* @return the day of the week of the anchor day of this `DoomsdayDate`'s year
|
* @return the day of the week of the anchor day of this `DoomsdayDate`'s year
|
||||||
*/
|
*/
|
||||||
getYearAnchorString(): string {
|
getYearAnchorString(): string {
|
||||||
const anchorDate = new Date(this.date);
|
return this.date.set({day: 4, month: 4}).setLocale("en-US").weekdayLong;
|
||||||
anchorDate.setDate(4); // 4th
|
|
||||||
anchorDate.setMonth(3); // April
|
|
||||||
anchorDate.setHours(0); // midnight local time
|
|
||||||
return DoomsdayDate.getWeekDayOf(anchorDate);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -306,41 +303,10 @@ class DoomsdayDate {
|
||||||
* @return the day of the week of this `DoomsdayDate`
|
* @return the day of the week of this `DoomsdayDate`
|
||||||
*/
|
*/
|
||||||
getWeekdayString(): string {
|
getWeekdayString(): string {
|
||||||
return DoomsdayDate.getWeekDayOf(this.date);
|
return this.date.setLocale("en-US").weekdayLong;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the week day of [date].
|
|
||||||
*
|
|
||||||
* @param date the date to get the week day of; if it is a `number`, then 0 corresponds to Sunday
|
|
||||||
* @return the name of the week day corresponding to [date]
|
|
||||||
*/
|
|
||||||
static getWeekDayOf(date: Date|number): string {
|
|
||||||
if (date instanceof Date) {
|
|
||||||
return date.toLocaleString("en-US", {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";
|
|
||||||
default:
|
|
||||||
throw new Error(`Unexpected weekday number '${date}'.`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the day of the week corresponding to `dayString`, or an empty string if no day was recognized.
|
* Returns the day of the week corresponding to `dayString`, or an empty string if no day was recognized.
|
||||||
*
|
*
|
||||||
|
@ -377,9 +343,9 @@ class DoomsdayDate {
|
||||||
*/
|
*/
|
||||||
static random() {
|
static random() {
|
||||||
// TODO Give custom date range to this method
|
// TODO Give custom date range to this method
|
||||||
const minDate = new Date("0001-01-01").getTime() / 86400000;
|
const startDate = DateTime.utc(1, 1, 1);
|
||||||
const maxDate = new Date("9999-12-31").getTime() / 86400000;
|
const dayRange = 9999 * 365 + (10000 / 400 * 97) - 1;
|
||||||
return new DoomsdayDate(new Date(generateRandom(minDate, maxDate) * 86400000));
|
return new DoomsdayDate(startDate.plus({days: generateRandom(0, dayRange)}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -467,7 +433,7 @@ doAfterLoad(() => {
|
||||||
|
|
||||||
updateTitle() {
|
updateTitle() {
|
||||||
if (yearDetails.isOpened())
|
if (yearDetails.isOpened())
|
||||||
this.titleLabel.innerText = `Doomsday of year ${quizDate.date.getFullYear()}?`;
|
this.titleLabel.innerText = `Doomsday of year ${quizDate.date.year}?`;
|
||||||
else
|
else
|
||||||
this.titleLabel.innerText = `Year`;
|
this.titleLabel.innerText = `Year`;
|
||||||
}
|
}
|
||||||
|
@ -491,7 +457,7 @@ doAfterLoad(() => {
|
||||||
}
|
}
|
||||||
|
|
||||||
updateTitle() {
|
updateTitle() {
|
||||||
this.titleLabel.innerText = `Weekday of ${quizDate.date.toISOString().substring(0, 10)}?`;
|
this.titleLabel.innerText = `Weekday of ${quizDate.date.toISODate()}?`;
|
||||||
}
|
}
|
||||||
}($("#day-input"), $("#day-title-label"), $("#day-submit"));
|
}($("#day-input"), $("#day-title-label"), $("#day-submit"));
|
||||||
|
|
||||||
|
@ -509,7 +475,7 @@ doAfterLoad(() => {
|
||||||
function reloadQuiz() {
|
function reloadQuiz() {
|
||||||
quizDate = DoomsdayDate.random();
|
quizDate = DoomsdayDate.random();
|
||||||
console.log("# Reset");
|
console.log("# Reset");
|
||||||
console.log(`New date: ${quizDate.date.toISOString().substring(0, 10)}`);
|
console.log(`New date: ${quizDate.date.toISODate()}`);
|
||||||
console.log(` ${quizDate.date}`);
|
console.log(` ${quizDate.date}`);
|
||||||
console.log(`Century#: ${quizDate.getCentury()}`);
|
console.log(`Century#: ${quizDate.getCentury()}`);
|
||||||
console.log(`Century: ${quizDate.getCenturyAnchorString()}`);
|
console.log(`Century: ${quizDate.getCenturyAnchorString()}`);
|
||||||
|
|
Loading…
Reference in New Issue