Blog Post

Javascript new Date() Returns NaN or is Blank in Safari

July 6, 2021

Category: Web Dev

Tags: js

Today I was looping through dates to create a custom calendar page for a client site. When I viewed the published site in Safari, I was surprised to find a blank section on my page. It worked everywhere else and Safari dev tools showed no errors.

The Problem: Date Format

I was using a date format Safari doesn’t support (FWIW, IE doesn’t either, but that isn’t a big deal).

const date = new Date('2021-08-09')

But yyyy-mm-dd isn’t supported …

The Solution: Change Date Formats

So far as I can tell, the following formats are supported by all browsers. Switching your date format to one of these should fix the problem.

const nDate = new Date('2021, 07, 06'); // yyyy, mm, dd
const nDate = new Date(2021, 07, 06, 10, 05, 00); // yyyy, mm, dd, hh, mm, ss
const nDate = new Date(07/06/2021); // mm/dd/yyyy
const nDate = new Date(07/06/2021 10:05:00); // mm/dd/yyyy hh:mm:ss
const nDate = new Date(1625600237781); // milliseconds
const nDate = new Date(Tue Jul 06 2021 10:05:00); // Day Mon dd yyyy hh:mm:ss