I am working on displaying a banner on a website, between two dates (the campaign's start date and end date).
For this purpose, I have the following function:
function checkCampaignActive() {
const campaignDateStart = new Date('2022-09-14');
const campaignDateStop = new Date('2022-09-15');
const today = Date.now();
const isCampaignStarted = today >= campaignDateStart.getTime();
const isCampaignExpired = today > campaignDateStop.getTime();
this.isCampaignActive = isCampaignStarted && !isCampaignExpired;
console.log('Is started: ', isCampaignStarted);
console.log('Is expired: ', isCampaignExpired);
console.log(isCampaignActive);
}
checkCampaignActive();
The problem
Since getTime() returns the milliseconds between Jan 1 1970, 00:00 GMT and 2022-09-14 and 00:00 GMT and my local time is not GMT, there is a gap between the desired campaign start and the real one.
Whatever new Date('2022-09-14') returns has to be adjusted for the local time, but remain an object, not a string.