Mike Meldrem Mike Meldrem - 6 months ago 16
Javascript Question

JavaScript to Output Text Based on User's Current Time

I don't know JavaScript, but I am familiar with following directions. I know a little PHP.

I'm looking for a piece of JS that will output a particular string of text for my header, based on the user's current time.

For example:

12:00AM - 12:00PM - Good Morning!
12:00PM - 6:00PM - Good Afternoon!
6:00PM - 12:00AM - Good Evening!

Answer

Try following piece of Javascript code:

var today = new Date()
var curHr = today.getHours()

if (curHr < 12) {
  console.log('good morning')
} else if (curHr < 18) {
  console.log('good afternoon')
} else {
  console.log('good evening')
}