I am building a schedule for a radio show and i need to show which show is currently live. At the moment it is working from the start hour but still shows "live" after the end hour.
This screenshot is taken at 10:12am
i am using the date-fns
library.
the schedule data shape is as follows.
"shows": [
{
"artist": "Set & Setting ",
"starts_at": "2020-06-29T07:00:00.000Z",
"ends_at": "2020-06-29T08:00:00.000Z",
"description": "w/ JD"
},
{
"artist": "Set & Setting ",
"starts_at": "2020-06-29T08:00:00.000Z",
"ends_at": "2020-06-29T09:00:00.000Z",
"description": "w/ JD"
},
{
"artist": "The Great Stuff",
"starts_at": "2020-07-16T21:00:00.000Z",
"ends_at": "2020-07-16T13:00:00.000Z",
"description": "w/ ??"
},
{
"artist": "The Melting Pot ",
"starts_at": "2020-07-16T17:00:00.000Z",
"ends_at": "2020-07-16T19:00:00.000Z",
"description": "w/ Tom B"
}
]
My logic for working out if the current item in the iteration is live is
...
import { startOfToday, parseISO, getHours, getMinutes, sub } from "date-fns"
...
const now = new Date();
const { starts_at, ends_at } = item;
const liveNow = (
(getHours(now) >= getHours(parseISO(starts_at)) && getMinutes(now) >= getMinutes(parseISO(starts_at))) &&
(getHours(now) <= getHours(parseISO(ends_at)) && getMinutes(now) <= getMinutes(sub(parseISO(ends_at), { minutes: 1 })))
);
Try to use: Before version 2.0.0:
isWithinRange(date, startDate, endDate)
Example:
const liveNow = isWithinRange(now, parseISO(starts_at), parseISO(ends_at))
After 2.0.0:
isWithinInterval(date, interval, [options])
Example:
const liveNow = isWithinInterval(
now,
{ start: parseISO(starts_at), end: parseISO(ends_at) }
)
Good luck!