How can I display different phrases depending on the time of day with javascript?

Kev*_*vin 2 javascript

For example, if it's between 8 and 1 pm, it would say "good morning", if it's 1:01-6:00 pm, it would say "good afternoon" and if it's between 6:01-11:59, it would say "good evening"

How do I go about doing that using javascript/jquery?

Dar*_*rov 5

您可以使用Date对象:

var date = new Date();
var hours = date.getHours();
if (hours >= 8 && hours < 13) {
    alert('Good Morning');
} else if (hours >= 13 && hours < 18) {
    alert('Good Afternoon');
} else if (hours > 18 && hours <= 23) {
    alert('Good Evening');
} else {
    alert('Don\'t disturb me at this lately hour');
}
Run Code Online (Sandbox Code Playgroud)