How do you check if it is monday and after 9 o'clock javascript

Viewed 27

I am trying to have an alert run only when it is a certain day AND it is after 9am but i cant work out how to do it, here is the code i already have:

var today = new Date().getHours();
var date = new Date();
if (today >= 9) {
    if(today.getDay() == 3) {
        alert('Alert 1');
    }

Thanks

1 Answers

First, create an instance of the built-in class Date, then extract the current day and hours:

const now = new Date();
var day = now.getDay();
var hours = now.getHours();

Now you can check if it's Monday and after 9am:

if (day == 1 && hours >= 9) {
    console.log("Yes");
} else {
    console.log("No");
}

Notes:

  • The day is an integer ranging from 0 to 6, 0 being Sunday and 6 being Saturday.

  • The hours is a integer ranging from 0 to 23.

Related