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

329
Views
parse function in date-fns returns one day previous value

I am trying to parse date using date-fns library but getting one day previous as result. How can I avoid this and get the right result?

start = '2021-08-16'
const parseStart = parse(start, 'yyyy-MM-dd', new Date());

output:

2021-08-15T18:30:00.000Z
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Not to have this timezone overhead I'd suggest formatting your date string into ISO string and then using parseISO function of date-fns.

Like this:

import { parseISO } from 'date-fns';

const parseStringDate = (dateString: string): Date => {
  const ISODate = new Date(dateString).toISOString();
  return parseISO(ISODate);
};

parseStringDate("2021-08-19") //2021-08-19T00:00:00.000Z
over 4 years ago · Santiago Trujillo Report

0

We had a similar problem while trying to use the date-fns format function on a hyphened string date. It was subtracting one day from the date while formating. The solution was in fact to change the hypes for slashes.

const dateString = '2010-08-03'
const date = new Date(dateString.replace(/-/g, '/'))
over 4 years ago · Santiago Trujillo Report

0

This worked for me (v.2+):

// wrong:
const date = format(new Date('2021-10-01'),'dd/MM/yyyy')
console.log(date) // "30/09/2021"

// ok:
const date = format(new Date('2021/10/01'),'dd/MM/yyyy')
console.log(date) // "01/10/2021"
over 4 years ago · Santiago Trujillo 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!