Skip to content

Instantly share code, notes, and snippets.

@AllanPooley
Created October 9, 2018 09:59
Show Gist options
  • Select an option

  • Save AllanPooley/c41f2e4a0e63e3eaf89b57f2570fcb55 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown
Author

@marcus-at-localhost
Copy link
Copy Markdown

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