Skip to content

Instantly share code, notes, and snippets.

@AllanPooley
Created October 9, 2018 09:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AllanPooley/c41f2e4a0e63e3eaf89b57f2570fcb55 to your computer and use it in GitHub Desktop.
Save AllanPooley/c41f2e4a0e63e3eaf89b57f2570fcb55 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';
}
@AllanPooley
Copy link
Author

@marcus-at-localhost
Copy link

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