Created
July 11, 2018 10:04
-
-
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!**
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
/** | |
* 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