Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
How to handle UTC math using date-fns library in Node?

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!