Skip to content

Instantly share code, notes, and snippets.

@James1x0
Created January 15, 2014 19:42
Show Gist options
  • Save James1x0/8443042 to your computer and use it in GitHub Desktop.
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!**
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 + ".";
*/
@eshwarenm
Copy link

Good stuff! Thanks!

@blaadje
Copy link

blaadje commented Mar 21, 2018

Cool

@krisan012
Copy link

thanks

@AllanPooley
Copy link

Thanks James!

ES6ified:

getGreetingTime = (currentTime) => {
  if (!currentTime || !currentTime.isValid()) { return 'Hello'; }

  const splitAfternoon = 12; // 24hr time to split the afternoon
  const splitEvening = 17; // 24hr time to split the evening
  const currentHour = parseFloat(currentTime.format('HH'));

  if (currentHour >= splitAfternoon && currentHour <= splitEvening) {
    // Between 12 PM and 5PM
    return 'Good afternoon';
  } else if (currentHour >= splitEvening) {
    // Between 5PM and Midnight
    return 'Good evening';
  }
  // Between dawn and noon
  return 'Good morning';
}

@hardikdabhi
Copy link

Nice contribution Allan.

Minor correction: I guess the 1st if condition should have currentHour < splitEvening instead of currentHour <= splitEvening. Or else anywhere between 5pm-6pm it'll make it afternoon. :)

@erosenberg
Copy link

Thanks, Allan!

@headfire94
Copy link

If someone looking for vanilla JS solution:

const renderWelcomeMsg = (currentTime = new Date()) => {
  const currentHour = currentTime.getHours()
  const splitAfternoon = 12; // 24hr time to split the afternoon
  const splitEvening = 17; // 24hr time to split the evening

  if (currentHour >= splitAfternoon && currentHour <= splitEvening) {
    // Between 12 PM and 5PM
    return 'Good afternoon';
  } else if (currentHour >= splitEvening) {
    // Between 5PM and Midnight
    return 'Good evening';
  }
  // Between dawn and noon
  return 'Good morning';
}

@yasso1am
Copy link

  greetingText = () => {
    const now = moment()
    const currentHour = now.hour()
      if (currentHour >= 12 && currentHour <=17) return "Good Afternoon,"
      else if (currentHour <= 18) return "Good Evening,"
      else return "Good Morning,"
  }

@krohne
Copy link

krohne commented Jan 15, 2019

Simplify the time interval:

if ( currentHour < 12 ) {
  // Before 12PM
  return 'Good morning';
} else if ( currentHour < 18 ) {
  // After 12pm, before 6PM
  return 'Good afternoon';
} else {
  // After 6PM
  return 'Good evening';
}

@kayoderock
Copy link

kayoderock commented Apr 3, 2019

@yasso1am, better still

    currentHour () {
      const currentHour = this.Moment().format("HH");
      if (currentHour == 0 || currentHour < 12) return "Good Morning"
      else if (currentHour <= 19) return "Good Afternon"
      else return "Good Evening"
    }

@damithg-dev
Copy link

damithg-dev commented Oct 6, 2019

@yasso1am, I have changed the code for the local timezone

greetingText = () => {
const now = moment()
const currentHour = now.local().hour()
if (currentHour >= 12 && currentHour <=17) return "Good Afternoon"
else if (currentHour <= 18) return "Good Evening"
else return "Good Morning"
}

@marquez138
Copy link

@yasso1am, I have changed the code for the local timezone

greetingText = () => {
const now = moment()
const currentHour = now.local().hour()
if (currentHour >= 12 && currentHour <=17) return "Good Afternoon"
else if (currentHour <= 18) return "Good Evening"
else return "Good Morning"
}

Newbie question, how would i format that for a react component?

@yasso1am
Copy link

yasso1am commented Oct 23, 2019

@marquez138 I'm not sure I understand your question. You could declare this function from inside your component and then call it inside of your return()

const ExampleComponent: React.FunctionComponent = () => {

  const greetingText = () => {
    const now = moment()
    const currentHour = now.local().hour()
    if (currentHour >= 12 && currentHour <=17) return "Good Afternoon"
    else if (currentHour <= 18) return "Good Evening"
    else return "Good Morning"
  }

  return (
    <div>
      <h1> {greetingText()} </h1> 
    </div>
  ) 

}

@damithg-dev
Copy link

@yasso1am, I have changed the code for the local timezone

greetingText = () => {
const now = moment()
const currentHour = now.local().hour()
if (currentHour >= 12 && currentHour <=17) return "Good Afternoon"
else if (currentHour <= 18) return "Good Evening"
else return "Good Morning"
}

Newbie question, how would i format that for a react component?

create a class named as a commonFunction

export const greetingText = () => { const now = moment(); const currentHour = now.local().hour(); if (currentHour >= 12 && currentHour <= 17) return 'Good Afternoon'; else if (currentHour <= 18) return 'Good Evening'; else return 'Good Morning'; };
to use

import {greetingText } from '../commonFunction' <Text>{greetingText()}</Text>

@deandex
Copy link

deandex commented Oct 30, 2019

  greetingText = () => {
    const now = moment()
    const currentHour = now.hour()
      if (currentHour >= 12 && currentHour <=17) return "Good Afternoon,"
      else if (currentHour <= 18) return "Good Evening,"
      else return "Good Morning,"
  }

Why 'Good evening' is <= 18, i think it should be >= 18.
Or i am wrong ?

@neyosoft
Copy link

neyosoft commented Nov 5, 2019

const currentHour = moment().format('HH');
const dayTime = currentHour < 12 ? 'Morning' : currentHour <= 17 ? 'Afternoon' : 'Evening';

@danielcolinjames
Copy link

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 😉

@ccraig09
Copy link

ccraig09 commented Jul 15, 2020

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

@lucidlive
Copy link

lucidlive commented Oct 7, 2020

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) {
...
}

@hmiiro
Copy link

hmiiro commented May 14, 2021

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!

@teamcoltra
Copy link

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.

@krohne
Copy link

krohne commented Oct 30, 2023

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment