Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Mar 1, 2018

Good stuff! Thanks!

@blaadje

This comment has been minimized.

Copy link

commented Mar 21, 2018

Cool

@krisan012

This comment has been minimized.

Copy link

commented Jul 2, 2018

thanks

@AllanPooley

This comment has been minimized.

Copy link

commented Oct 9, 2018

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

This comment has been minimized.

Copy link

commented Oct 28, 2018

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

This comment has been minimized.

Copy link

commented Nov 29, 2018

Thanks, Allan!

@headfire94

This comment has been minimized.

Copy link

commented Dec 19, 2018

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

This comment has been minimized.

Copy link

commented Dec 25, 2018

  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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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"
    }
@gdamith

This comment has been minimized.

Copy link

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"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.