I'm trying to use the date-fns library to add and subtract dates, but it doesn't handle UTC dates correctly. For example:
> const { add } = require('date-fns');
undefined
> s = new Date('2021-03-27')
2021-03-27T00:00:00.000Z
> add(s, {days:1})
2021-03-28T00:00:00.000Z
> add(s, {days:2})
2021-03-28T23:00:00.000Z
The issue is that my dates are in UTC, and I want to do UTC date arithmetic on them, but as soon as they're converted to a date/time they get treated as local dates. This is a problem. How can I solve this?
You can make all your operations, and at the end of your function, call the method toISOString() of the final date object. It'll return the UTC 0 value.
And remember that the date object you've passed by parameter to add function is never changed, the returned value is what you want.