0
Thanks

A few words of thanks would be greatly appreciated.

Javascript - Change greeting based on time of day





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> 


0
Thanks

A few words of thanks would be greatly appreciated.

Ask a question
Jean-François Pillou

CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jeff Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.

Learn more about the CCM team

Original article published by . Translated by deri58. Latest update on by deri58.

This document, titled "Javascript - Change greeting based on time of day," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (https://ccm.net/).

0 Comments