Forked from James1x0/momentjs.humanized.triplesplit.time.js
Created
January 17, 2018 11:54
-
-
Save msonowal/ad3e376764702f2f526fb929ddfb1705 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 hidden or 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