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

154
Views
parse date-fns returns one day previous value

I am trying to parse date using date-fns library for the first time (I removed moment.js and I will introduce date-fns in my project) but, when I use parse function, I get one day previous as result. I read in this topic

parse function in date-fns returns one day previous value

that problem is due to timezones but I could not be able to fix my problem because I receive dates in the format "yyyyMMdd". This is my code

var start ="20210119";
var stop ="20210130";

const dateFrom = parse(start, "yyyyMMdd", new Date());
console.log("date form",dateFrom);

Output of my console log is: 2021-01-18T23:00:00.000Z

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

0

Your dateFrom date is actually correct, assuming you want it in your local time.

The reason it's showing an hour earlier in your console output is because it is being displayed in UTC time (hence the 'Z' for Zulu at the end). I assume your local time is one hour ahead of UTC in January.

We can use Date.toLocaleString() to output the time in either UTC or local time.

If we output in local time, we see the date is actually correct (2021-01-19 00:00:00), and if we output in UTC (set the timeZone to 'UTC') we see it is one hour earlier (as in your console output).

We can also output the local date using Date.getFullYear(), Date.getMonth() and Date.getDate().

var start ="20210119";
var stop ="20210130";

const dateFrom = dateFns.parse(start, "yyyyMMdd", new Date());

console.log("Date from (is Date object):", dateFrom instanceof Date);

console.log("\nDate from (console.log):",dateFrom);
console.log("Date from (UTC):", dateFrom.toLocaleString([], { timeZone: 'UTC' }));

   
console.log("\nDate from (Local):", dateFrom.toLocaleString());
console.log("Date from (Local):", dateFrom.toDateString());
console.log("Date from (Local):", `${dateFrom.getFullYear()}-${dateFrom.getMonth()+1}-${dateFrom.getDate()}`);
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.28.5/date_fns.min.js"></script>

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!