Identifying the current day is a simple task, even without relying on moment.js:
console.log("Happy " + ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"][new Date().getUTCDay()-1])
Here's a more intricate example below:
var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
var day = new Date().getUTCDay() - 1;
function getDayMessage() {
switch (day) {
case 0:
return "Moody " + days[day];
break;
case 1:
return "Trippy " + days[day];
break;
case 2:
return "Weeping " + days[day];
break;
case 3:
return "Thundering " + days[day];
break;
case 4:
return "Freaky " + days[day];
break;
case 5:
return "Salty " + days[day];
break;
case 6:
return "Super " + days[day];
break;
default:
return "Happy " + days[day];
}
}
var classes = document.getElementById("messageOfTheDay").getAttribute('class').split(' ');
classes.push('day-' + days[day].toLowerCase());
document.getElementById("messageOfTheDay").setAttribute('class', classes.join(' '))
document.getElementById("messageOfTheDay").innerHTML = getDayMessage();
console.log("Below is the HTML code for or `h1`. Notice the added day-{DAY-OF-WEEK}");
console.log(document.getElementById("messageOfTheDay"));
<h1 id="messageOfTheDay" class="hello world"></h1>
The previous snippet has been updated to a function that returns the string. Now you can easily choose to alert
, console.log
, or insert it into your page as I did above.