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

242
Views
Why does this produce NaN

When I try this it produces NaN

const date1 = new Date('29/11/2021');
const date2 = new Date('30/11/2021');
const diffTime = Math.abs(date2 - date1);

but this works

const date1 = new Date('7/13/2010');
const date2 = new Date('12/15/2010');
const diffTime = Math.abs(date2 - date1);

Could someone please explain why?

I got the code from this question but when I tried it with my dates it didn't work

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

0

Actually, producing NaN is browser dependant. Some browsers could understand new Date('29/11/2021') as 29 monthes = 12+12+5 => May 11 2023

To be sure you could use more parsing friendly

new Date('November 29, 2021');

or even

new Date(2021, 10, 29); // its important, that in such constructor form months start from 0
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!