lundi 23 novembre 2020

Timezone sensitive date comparisons in Javascript

I'm determining the difference between two calendar dates in Javascript in an Ember app. I'm currently using date-fns's differenceInCalendarDays.

Now that mostly provides the desired results, the only issue is that I need to handle calculating the difference between 2 dates that is sensitive to a timezone (not the local timezone to the browser).

As far as I'm aware JS Dates are tracked as UTC, with no timezone stored on the date object itself. Any timezone localization I've done in JS has been outputting a string. Is there a good library or way to accomplish differenceInCalendarDays while taking into account the timezone?

const daysAgo = this.intl.formatRelative(-differenceInCalendarDays(new Date(), someOtherDay), {
    unit: 'day', 
    numeric: 'auto', 
    timeZone: 'this.intl.timeZone
});

This is a small sample of what I'm doing, obviously differenceInCalendarDays will resolve to a number which won't take into account any timeZone. The docs for differenceInDays is timezone sensitive to the browser's local time (which is not helpful here), but differenceInCalendarDays makes no such mention. Any help would be greatly appreciated!




Aucun commentaire:

Enregistrer un commentaire