Home / Dynamic Greeting
Duplicate Snippet

Embed Snippet on Your Site

Dynamic Greeting

Use as Shortcode to display a greeting based on the time of day.

30+
Code Preview
html
<div id="greeting-message"></div>
<script type="text/javascript">
	document.addEventListener( 'DOMContentLoaded', function () {
		const morningGreeting = 'Good Morning!';
		const afternoonGreeting = 'Good Afternoon!';
		const eveningGreeting = 'Good Evening!';
		const date = new Date();
		const hour = date.getHours();
		let greeting;
		if ( hour >= 5 && hour < 12 ) {
			greeting = morningGreeting;
		} else if ( hour >= 12 && hour < 18 ) {
			greeting = afternoonGreeting;
		} else {
			greeting = eveningGreeting;
		}
		document.getElementById( 'greeting-message' ).textContent = greeting;
	} );
</script>

Comments

Add a Comment