I’m trying to make a Livestream feature in my project, I have an array of programs (videos), every video has startDate and endDate. I should run every video when its time has come.
const programs = [
{ video: "URL", startDate: 1644405993, endDate: 1644406032 },
{ video: "URL", startDate: 1644405993, endDate: 1644406032 },
{ video: "URL", startDate: 1644405993, endDate: 1644406032 },
{ video: "URL", startDate: 1644405993, endDate: 1644406032 }
];
>Solution :
You can try this
const programs = [
{ video: "URL", startDate: 1644405993, endDate: 1644406032 },
{ video: "URL", startDate: 1644405993, endDate: 1644406032 },
{ video: "URL", startDate: 1644405993, endDate: 1644406032 },
{ video: "URL", startDate: 1644405993, endDate: 1644406032 }
];
let currentTime = new Date().toLocaleTimeString("en-US");
for(let pg of programs){
let startDate = new Date(pg.startDate).toLocaleTimeString("en-US");
let endDate = new Date(pg.endDate).toLocaleTimeString("en-US");
console.log({
startDate:startDate,
endDate:endDate,
currentTime:currentTime
})
if (currentTime >= startDate && currentTime < endDate){
console.log("Current time in between start and end time");
}
}