I have a timezone map with publishing hour in the local zone with news that must define when they should be published on a date using a date picker.
This is a new news article
that is initialized with the following:
{ timeZoneId: 'Europe/Paris, releaseHour: 9, publishingDateTime: undefined }
// 9 is the hour GMT+1
I want to know how can I from const now = new Date()
, verify if this article should be
published today or the next day, the criteria are:
- Is
now
beforereleaseHour
? (is 9am GMT+1 in paris already passs or not) - If yes, then we should offer the next release slot at 9am GMT+1 + 1 day
- If no, then we should use the release slot at 9am the same day
How is this possible?
This is how I have tried:
import { isBefore, isEqual } from 'date-fns';
import { utcToZonedTime } from 'date-fns-tz';
export const getNextPublishingDateTime = (now, timeZoneId, releaseHour) => {
const zoned = utcToZonedTime(now, timeZoneId);
const releaseTime = new Date(zoned.toISOString());
releaseTime.setHours(releaseHour, 0, 0, 0);
if (isBefore(zoned, releaseTime) || isEqual(zoned, releaseTime)) {
console.log('before');
return releaseTime;
}
releaseTime.setDate(releaseTime.getDate() + 1);
console.log('after');
return releaseTime;
};
But the hour returned by utcToZonedTime
is not +01:00 offset, instead it is a date at my offset.
I have tried some other ideas, using moment-tz
and vanilla Date
, I found this task a bit complicated and hope to find help with the JS community as this look to be a normal date comparaison.