Created
January 15, 2014 19:42
-
-
Save James1x0/8443042 to your computer and use it in GitHub Desktop.
Get a humanized, "Morning", "Afternoon", "Evening" from moment.js
**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 (m) { | |
var g = null; //return g | |
if(!m || !m.isValid()) { return; } //if we can't find a valid or filled moment, we return. | |
var split_afternoon = 12 //24hr time to split the afternoon | |
var split_evening = 17 //24hr time to split the evening | |
var currentHour = parseFloat(m.format("HH")); | |
if(currentHour >= split_afternoon && currentHour <= split_evening) { | |
g = "afternoon"; | |
} else if(currentHour >= split_evening) { | |
g = "evening"; | |
} else { | |
g = "morning"; | |
} | |
return g; | |
} | |
/* USE | |
//The var "humanizedGreeting" below will equal (assuming the time is 8pm) "Good evening, James." | |
var user = "James"; | |
var humanizedGreeting = "Good " + getGreetingTime(moment()) + ", " + user + "."; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fair enough, but let's just shift midnight by 4 hours, and simplify the expressions.