Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if current date is between two dates in javascript

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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");
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading