Skip to content

Instantly share code, notes, and snippets.

@rafaell-lycan
Created July 11, 2018 10:04
Show Gist options
  • Save rafaell-lycan/67500dce7205e25461e62fcebd604e33 to your computer and use it in GitHub Desktop.
Save rafaell-lycan/67500dce7205e25461e62fcebd604e33 to your computer and use it in GitHub Desktop.
Get a humanized, "Morning", "Afternoon", "Evening" in Javascript. **Great for user greetings!**
/**
* Function getGreetingTime
* It returns a greet string based on the current time.
* @returns {string}
*
* Usage:
*
* const greetings = `Good ${getGreetingTime()}, James.`
* Assuming the time is 8pm, the output above will be "Good evening, James."
*/
function getGreetingTime() {
const currentHour = new Date().getHours();
const splitAfternnon = 12;
const splitEvening = 18;
if(currentHour < splitAfternnon) return 'morning';
if(currentHour < splitEvening) return 'afternoon';
return 'evening';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment