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

74
Views
Showing NaN on Converting 12 HR format String to Date in Javascript in Firefox

Let's say I've to convert this string into a Javascript Date object with this line:

new Date('2001-01-31 02:00:00 pm')

In Chrome it is fine. But in Firefox, it shows an Invalid Date. Why is that? I handled it manually, parsed the Hour part and added 12 hours when needed. But, is there any other way to convert this?

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

0

I think issue you are facing is to do with differences between Chromium based browsers and the Gecko based firefox. Thus they handle your string a little differently from each other. As for converting a string into a Date object, the only way I can think of is by parsing it, ie passing it into Date's constructor (as you have done).

Here is how I would solve this issue so it works in all browsers (by using the built-in methods inside the Date object).

const myDate = new Date();
myDate.setFullYear(2001);
myDate.setUTCMonth(0);
myDate.setUTCDate(31);
myDate.setUTCHours(0);
myDate.setUTCMinutes(0);
myDate.setUTCSeconds(0);
myDate.setUTCMilliseconds(0);


console.log(myDate); // 2001-01-31T02:00:00.000Z

// Now add a year:
const oldYear = myDate.getFullYear();
myDate.setFullYear(oldYear + 1);



// Results:
console.log("Expected result: year is 2002");
console.log(myDate); // Year is 2001 + 1 = 2002

Of course in your use case you may want to add hours, months or any other amount of time, so you can adapt accordingly.

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!