Created
April 28, 2021 20:10
-
-
Save edbella/7f8be7ad4b4d5538ae2a51b958ab9d0f to your computer and use it in GitHub Desktop.
Returns a specific string that greets based on the time of day
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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