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

175
Views
Parsing ISO-Date from Wikidata in JS

What possibilities are there to parse an ISO string of a date in Javascript, if the date can also be B.C.?
I have dates from Wikidata that look like this, for example (but also other ISO-formats are possible) : -0709-01-01T00:00:00Z

This is too far back for Date.parse, and even libraries like Luxon or Moment can't do anything with it here.
I would like to be able to query year, month, day, hour and minute in a desired time zone.

I could now do this by creating a regular expression and parse the string with it, but with the many possible variations I see the danger of not including everything.
I also haven't found a regex on the internet that can handle everything.

Does anyone know a library or another solution that I could use here?

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

0

"-0709-01-01T00:00:00Z" is not consistent with the format defined in ECMA-262 so parsing is implementation dependent.

Per ECMA-262, where the year has a sign (i.e. preceded by + or -) it must use the expanded year format and have 6 digits, e.g.

new Date('-000709-01-01T00:00:00Z')

works in Safari, Firefox and Chrome at least.

So you can parse the string yourself or expand the year to 6 digits. The latter requires parsing and reformatting, so you might as well just parse it and avoid the built–in parser by going directly to the constructor, e.g.

function parseUTC(d) {
  let [Y, M, D, H, m, s] = d.match(/\d+/g);
  let sign = /^-/.test(d)? -1 : 1;
  return new Date(Date.UTC(sign*Y, M-1, D, H, m, s));
}

let d = '-0709-01-01T00:00:00Z';

// -000709-01-01T00:00:00.000Z
console.log(parseUTC(d).toISOString());

// Alternatively…
console.log(new Date(d.replace(/^-/,'-00')).toISOString());

While the second method "works", I think the first method is more robust as it's not dependent on the number of digits in the year, whereas the second assumes it's 4 digits and doesn't handle say "+2020-10-23T00:00:00Z".

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!