the-glorious-startpage/js/greeter-date-message.js

61 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-06-09 04:02:03 +02:00
class GreeterDateMessage {
2020-06-09 12:13:12 +02:00
2020-06-09 04:02:03 +02:00
constructor() {
this._greeterMessage = document.querySelector('#greeterMessage');
this._dateMessage = document.querySelector('#dateMessage');
this._monthsArr = [
2020-06-09 04:02:03 +02:00
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
this._daysArr = [
2020-06-09 04:02:03 +02:00
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
];
this._updateGreeterDateMessage();
2020-06-09 04:02:03 +02:00
}
_getDayOrdinal = (day) => {
return day + (day > 0 ? ['th', 'st', 'nd', 'rd'][(day > 3 && day < 21) ||
day % 10 > 3 ? 0 : day % 10] : '');
}
2020-06-09 04:02:03 +02:00
_updateGreeterDateMessage = () => {
const date = new Date();
const hour = date.getHours();
let greeterSuffix = '';
if (hour >= 6 && hour < 12) {
greeterSuffix = 'Morning';
} else if (hour >= 12 && hour < 18) {
greeterSuffix = 'Afternoon';
} else {
greeterSuffix = 'Evening';
}
this._greeterMessage.innerText = `Good\n${greeterSuffix}!`;
this._dateMessage.innerText = `Today's the ${this._getDayOrdinal(date.getDate())} of ` +
`${this._monthsArr[date.getMonth()]}, and it's ${this._daysArr[date.getDay()]}.`;
2020-06-09 04:02:03 +02:00
}
2020-06-04 04:45:26 +02:00
2020-06-09 04:02:03 +02:00
}