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!
Demo: http://jsfiddle.net/DerekL/we8Ty/
Try this js code, This should work..
I know this is an old thread, i'm just sharing this 2 lines of code for anyone who needs simple solution :)
This is just a small variation of the solution from Derek 朕會功夫 above.
I felt the array is cleaner than a bunch of
if
statements.If you work the hours backwards, you don't need a start AND end hour.
Also, once you make a match, I added a
break;
to kick out early.How about:
Or augment
Date
see jsfiddle
With moment.js & ES6:
Adapted from James1x0's gist.