Published unlisted
Edited
Sep 6, 2021
Insert cell
Insert cell
re = /^([-+]\d{2})?\d{4}(-\d{2}(-\d{2})?)?(T\d{2}:\d{2}(:\d{2}(.\d{3})?)?(Z|[-+]\d{2}:\d{2})?)?$/
Insert cell
parser = (x) => (re.test(x) ? new Date(x) : new Date(x == null ? NaN : +x))
Insert cell
dates = `
2021-09-06T06:57:38
2021-09-06T06:57:38+00:00
2021-09-06T06:57:38.123+00:00
2021-09-06T06:57:38Z
+012000-09-01
-000100-09-01T02:02
1402
2008
2008-03
2021-09-06
2021T06:57:38Z
`
.split(/\n/)
.filter((d) => d)
Insert cell
tests = dates.map((d) => [
d,
d3.isoParse(d),
parser(d),
+d3.isoParse(d) === +parser(d)
])
Insert cell
tests.every(([, , , x]) => x) ? "👍" : "❌"
Insert cell
Insert cell
["99-02", "12"].map((d) => [d, d3.isoParse(d), parser(d)])
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more