Created
October 9, 2018 09:59
-
-
Save AllanPooley/c41f2e4a0e63e3eaf89b57f2570fcb55 to your computer and use it in GitHub Desktop.
Time based user greetings (Good Morning, Afternoon, Evening) using moment.js
This file contains 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'; | |
} |
Instead of moment.js currentTime.format('HH')
you could use new Date().getHours()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by https://gist.github.com/James1x0/8443042