Skip to content

Instantly share code, notes, and snippets.

@granmoe
Created May 9, 2017 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save granmoe/4b4a7ef0f7cacf511221413342c5e523 to your computer and use it in GitHub Desktop.
Save granmoe/4b4a7ef0f7cacf511221413342c5e523 to your computer and use it in GitHub Desktop.
Returns a message describing the time of day given a start and end hour. A small and humble chunk of code, but I thought my approach was kind of nifty.
function getTimeOfDay (start, end) {
if (start < 12 && end > 16) { return null } // not specific enough
// we don't care about the window from 22:00 - 06:00
const timesOfDay = Array(24)
.fill('Morning', 6, 12)
.fill('Afternoon', 12, 16)
.fill('Evening', 16, 22)
return Array.from(new Set(timesOfDay.slice(start, end)))
.filter(value => !!value)
.join(' / ')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment