Skip to content

Instantly share code, notes, and snippets.

@raymelon
Forked from AllanPooley/moment-greetings.js
Created January 2, 2021 07:39
Show Gist options
  • Save raymelon/7b830d7b567bf25a100a92aba6878fe5 to your computer and use it in GitHub Desktop.
Save raymelon/7b830d7b567bf25a100a92aba6878fe5 to your computer and use it in GitHub Desktop.
Time based user greetings (Good Morning, Afternoon, Evening) using moment.js
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