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.
Returns a specific string that greets based on the time of day
/**
* 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;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment