Get time of day using Javascript

This script segment prints a short greeting based on the time of day.

var now = new Date()
var hour = now.getHours()
var text = " "
if (hour < 12)
text = " morning"
else
if (hour < 16)
text = " afternoon"
else
if (hour < 20)
text = " evening"
else
text = " night"
document. write(" Good " + text + " ! " )

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.