# Date formatter returns NaN on mobile

**URL:** <https://talk.observablehq.com/t/date-formatter-returns-nan-on-mobile/2966>\
**Category:** Uncategorized\
**Created:** [March 24, 2020, 1:21pm UTC](https://talk.observablehq.com/t/date-formatter-returns-nan-on-mobile/2966 "2020-03-24T13:21:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkfreeman](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mkfreeman/32/2988_2.png) [@mkfreeman](https://talk.observablehq.com/u/mkfreeman)\
**Post date:** [March 24, 2020, 1:21pm UTC](https://talk.observablehq.com/t/date-formatter-returns-nan-on-mobile/2966/1 "2020-03-24T13:21:51Z")

</div>

This [notebook](https://observablehq.com/@mkfreeman/recreating-nyt-u-s-cases-map) displays formatted date objects below each chart using the following code:

```auto
// Store a formatted version to display below each chart
      const date_pieces = date.split("/");
      const date_str = date_pieces[0] + " " + date_pieces[1];
      obj.x_min_label = parseDate(new Date(date_str));

```

This works just fine on desktop, but returns `NaN` on mobile (strange…). Any thoughts?

![mobile_error](https://canada1.discourse-cdn.com/flex030/uploads/observablehq/original/2X/f/ff789afbfaa1824e8c6aff8900d26322577e580e.png)

---

<div class="post-metadata">

**Author:** ![severo](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/severo/32/786_2.png) [@severo](https://talk.observablehq.com/u/severo)\
**Post date:** [March 24, 2020, 2:05pm UTC](https://talk.observablehq.com/t/date-formatter-returns-nan-on-mobile/2966/2 "2020-03-24T14:05:38Z")

</div>

You’re basically doing:

```auto
new Date('3 10')

```

that returns “Invalid date”. It would be better to pass the date to new Date as an ISO8601 string to be sure to parse it as expected.

Note: I get NaN too on desktop, using Firefox. Maybe the configured locale can be an issue in your case.

---

<div class="post-metadata">

**Author:** ![severo](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/severo/32/786_2.png) [@severo](https://talk.observablehq.com/u/severo)\
**Post date:** [March 24, 2020, 2:07pm UTC](https://talk.observablehq.com/t/date-formatter-returns-nan-on-mobile/2966/3 "2020-03-24T14:07:41Z")

</div>

Or much simpler, as you already have year, month and day, to pass these values as the three first arguments. See [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global\_Objects/Date/Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date). Be careful that the month indexes must start at 0.

---

<div class="post-metadata">

**Author:** ![mkfreeman](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mkfreeman/32/2988_2.png) [@mkfreeman](https://talk.observablehq.com/u/mkfreeman)\
**Post date:** [March 24, 2020, 2:18pm UTC](https://talk.observablehq.com/t/date-formatter-returns-nan-on-mobile/2966/4 "2020-03-24T14:18:40Z")

</div>

Great, thanks! Strange how different browsers // operating systems handle dates differently. Appreciate it!

---

<div class="post-metadata">

**Author:** ![Job](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/job/32/1185_2.png) [@Job](https://talk.observablehq.com/u/Job)\
**Post date:** [March 25, 2020, 9:00am UTC](https://talk.observablehq.com/t/date-formatter-returns-nan-on-mobile/2966/5 "2020-03-25T09:00:38Z")

</div>

Usually that’s a left-over from before proper standardization. When in doubt it’s a good idea to check MDN (like the link severo gave) to see what works now, and to double check if you’re dealing with a stable or unstable API.
