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



Question:

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?


Solution:1

You could use the Date object:

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');  }  


Solution:2

Get current hour and minute like this

var d = new Date();    var curr_hour = d.getHours();  var curr_min = d.getMinutes();  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »