DEV Community

Julio Lugo
Julio Lugo

Posted on

differenceInDays not working. Timezones and date-fns classic issues

And yet another classic issue that's found when working with TS/JS and date-fns is the one-day difference issue. I know, what a mess. But I came into help: there's a solution for it.

In order to fix this, you just need to use these beautiful sequences over here and everything is solved:

export const fixTimezoneDate = (date: Date): Date => {
  return new Date(date.getTime() + date.getTimezoneOffset() * 60000)
}

// In your code

const someDate = fixTimezoneDate(new Date(Date.parse(start)))
const today = startOfDay(new Date()) // We use date-fns' startOfDate in case we want to handle today's info as well

Enter fullscreen mode Exit fullscreen mode

And that's it! A classic problem that can be solved as well

Top comments (0)