-
-
Save James1x0/8443042 to your computer and use it in GitHub Desktop.
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 + "."; | |
*/ |
const currentHour = moment().format('HH');
const dayTime = currentHour < 12 ? 'Morning' : currentHour <= 17 ? 'Afternoon' : 'Evening';
To anyone finding this from Google like I did, I set mine up like this:
const currentHour = new Date().getHours();
const greetingMessage =
currentHour >= 4 && currentHour < 12 ? // after 4:00AM and before 12:00PM
'Good morning.' :
currentHour >= 12 && currentHour <= 17 ? // after 12:00PM and before 6:00pm
'Good afternoon.' :
currentHour > 17 || currentHour < 4 ? // after 5:59pm or before 4:00AM (to accommodate night owls)
'Good evening.' : // if for some reason the calculation didn't work
'Welcome'
console.log(greetingMessage)
Because I think people looking at their computer or phone between 12AM and 4AM are more likely staying up late than waking up early 😉
To anyone finding this from Google like I did, I set mine up like this:
const currentHour = new Date().getHours(); const greetingMessage = currentHour >= 4 && currentHour < 12 ? // after 4:00AM and before 12:00PM 'Good morning.' : currentHour >= 12 && currentHour <= 17 ? // after 12:00PM and before 6:00pm 'Good afternoon.' : currentHour > 17 || currentHour < 4 ? // after 5:59pm or before 4:00AM (to accommodate night owls) 'Good evening.' : // if for some reason the calculation didn't work 'Welcome' console.log(greetingMessage)Because I think people looking at their computer or phone between 12AM and 4AM are more likely staying up late than waking up early 😉
Works like a cham, thanks @danielcolinjames
I did a little spin off of @danielcolinjames
const MORNING = 'MORNING';
const AFTERNOON = 'AFTERNOON';
const EVENING = 'EVENING';
const getTimeOfDay = () => {
const currentHour = new Date().getHours();
const MORNING = 'MORNING', AFTERNOON = 'AFTERNOON', EVENING = 'EVENING';
const results = [ MORNING, AFTERNOON, EVENING ];
const idx = currentHour >= 4 && currentHour < 12 ? 0 : currentHour >= 12 && currentHour <= 17 ? 1 : 2
return results[idx];
}
export { getTimeOfDay, MORNING, AFTERNOON, EVENING }
I then use it like:
if (getTimeOfDay() === MORNING) {
...
}
To anyone finding this from Google like I did, I set mine up like this:
const currentHour = new Date().getHours(); const greetingMessage = currentHour >= 4 && currentHour < 12 ? // after 4:00AM and before 12:00PM 'Good morning.' : currentHour >= 12 && currentHour <= 17 ? // after 12:00PM and before 6:00pm 'Good afternoon.' : currentHour > 17 || currentHour < 4 ? // after 5:59pm or before 4:00AM (to accommodate night owls) 'Good evening.' : // if for some reason the calculation didn't work 'Welcome' console.log(greetingMessage)Because I think people looking at their computer or phone between 12AM and 4AM are more likely staying up late than waking up early 😉
Haha...the best and easy!
I'm just popping in to give my thanks to OP and all the comments below. It's fun seeing everyone riff on such a simple code snippit.
To anyone finding this from Google like I did, I set mine up like this:
const currentHour = new Date().getHours(); const greetingMessage = currentHour >= 4 && currentHour < 12 ? // after 4:00AM and before 12:00PM 'Good morning.' : currentHour >= 12 && currentHour <= 17 ? // after 12:00PM and before 6:00pm 'Good afternoon.' : currentHour > 17 || currentHour < 4 ? // after 5:59pm or before 4:00AM (to accommodate night owls) 'Good evening.' : // if for some reason the calculation didn't work 'Welcome' console.log(greetingMessage)Because I think people looking at their computer or phone between 12AM and 4AM are more likely staying up late than waking up early 😉
Fair enough, but let's just shift midnight by 4 hours, and simplify the expressions.
let currentHour = new Date().getHours();
if (currentHour < 4) { // After midnight, but before 4am
currentHour = currentHour + 24
}
const greetingMessage =
currentHour < 12 ? // after 4:00AM and before 12:00PM
'Good morning.' :
currentHour < 18 ? // after 12:00PM and before 6:00pm
'Good afternoon.' :
currentHour < 28 ? // // after 5:59pm or before 4:00AM (to accommodate night owls)
'Good evening.' : // if for some reason the calculation didn't work
'Welcome'
Why 'Good evening' is <= 18, i think it should be >= 18.
Or i am wrong ?