-
-
Save raymelon/7b830d7b567bf25a100a92aba6878fe5 to your computer and use it in GitHub Desktop.
Time based user greetings (Good Morning, Afternoon, Evening) using moment.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getGreetingTime = (currentTime) => { | |
if (!currentTime || !currentTime.isValid()) { return 'Hello'; } | |
const splitAfternoon = 12; // 24hr time to split the afternoon | |
const splitEvening = 17; // 24hr time to split the evening | |
const currentHour = parseFloat(currentTime.format('HH')); | |
if (currentHour >= splitAfternoon && currentHour <= splitEvening) { | |
// Between 12 PM and 5PM | |
return 'Good afternoon'; | |
} else if (currentHour >= splitEvening) { | |
// Between 5PM and Midnight | |
return 'Good evening'; | |
} | |
// Between dawn and noon | |
return 'Good morning'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment