Javascript - Change greeting based on time of day

December 2016




Introduction


As with PHP, Javascript allows you to change the greeting based on the time of the day, thus you can change a "Good morning " to "Good evening" according to the clock of the user. But on the contrary of a PHP script, the advantage of the Javascript function is that that it follows the clock the user and not the server time.=

Implementation


Insert this snippet of code into your HTML source:

<body> 
[...] 
<span id="Good morning">Good morning</span> 
[...] 

<script type="text/javascript"> 
today=new Date() 
if(today.getHours() >= 0 && today.getHours() < 18) 
{      
document.getElementById('bonjour').innerHTML='Good morning'; 
} 
else 
{      
document.getElementById('bonjour').innerHTML='Good evening'; 
} 
</script> 

[...] 
</body> 



Related :

This document entitled « Javascript - Change greeting based on time of day » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.