Skip to content

Instantly share code, notes, and snippets.

@edbella
Created April 28, 2021 20:10
Show Gist options
  • Select an option

  • Save edbella/7f8be7ad4b4d5538ae2a51b958ab9d0f to your computer and use it in GitHub Desktop.

Select an option

Save edbella/7f8be7ad4b4d5538ae2a51b958ab9d0f to your computer and use it in GitHub Desktop.

Revisions

  1. edbella created this gist Apr 28, 2021.
    31 changes: 31 additions & 0 deletions getGreetingByTime.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    /**
    * Assumes you are using dayjs
    * Buy ideally you can use any date formatting library or the native JavaScript Date Object Constructor
    */

    /**
    * Gets the correct greeting time based now
    *
    * @returns {string} Greeting text
    */
    export const getGreetingTime = () => {
    const dateNow = dayjs().format("HH");

    // const dateNow = new Date().getHours(); - use this if you want to go the native route

    let g;

    const splitAfternoon = 12; // 24hr time to split the afternoon
    const splitEvening = 17; // 24hr time to split the evening
    const currentHour = parseFloat(dateNow);

    if (currentHour >= splitAfternoon && currentHour <= splitEvening) {
    g = "Good Afternoon";
    } else if (currentHour >= splitEvening) {
    g = "Good Evening";
    } else {
    g = "Good Morning";
    }

    return g;
    };