'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 1:[1]
You can check by calling this function.
function compareDates(startDate,endDate){
let current=new Date();
return startDate>=current && endDate<=current;
}
Solution 2:[2]
try this
export const checkTime = (startDate, endDate) => {// startDate= 1644405993, endDate = 1644406032
const now = new Date()
const bookingStartDate = new Date(startDate) // use this step to convert your date if its stored in a different format
const bookingEndDate = new Date(endDate) // use this step to convert your date if its stored in a different format
return now.getTime() > bookingStartDate.getTime() && now.getTime() < bookingEndDate.getTime()
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Asad Haroon |
| Solution 2 | amo |
