0

I am using the date-fns npm package for some date utility. Everything is working fine in the chrome browser. but in mac-safari it's not working. Here is my code snap...

const format = require('date-fns/format');
const parseISO = require('date-fns/parseISO');

const startDateInput = "2020-11-20T09:30:00.000Z";

const formattedStartDate = new Date(format(parseISO(startDateInput), 'MM-dd-yyyy'));
const formattedTodayDate = new Date(format(new Date(), "yyyy-MM-dd"))

console.log('formattedStartDate', formattedStartDate);//IN MAC SAFARI --> Invalid Date 
console.log('formattedTodayDate date1', formattedTodayDate);//IN MAC SAFARI --> Invalid Date 

The same console in chrome is giving Fri Nov 20 2020 18:30:00 GMT+0530 (India Standard Time) a valid date.

Not getting what is the issue with safari browser?

SMJ
  • 1
  • 2
  • Yeah safari is a pain for iso dates – Lk77 May 05 '22 at 12:18
  • `new Date(format(parseISO(startDateInput), 'MM-dd-yyyy'))` is absurd. Given the format of the timestamp referenced by *startDateInput* is supported by ECMA-262, use `new Date(startDateInput)`. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG May 05 '22 at 14:18

1 Answers1

-1

I think you don't need the format function if you just want a Date(), and you don't need to do new Date() either

This should work :

const startDate = parseISO(startDateInput);

Now you should have a valid Date object since date-fns will handle the ISO 8601 for you

if you want to format it :

const formattedStartDate = format(startDate, 'yyyy-MM-dd')

Here you have a date string, like 2022-05-05, you should just keep it as a string, use it where you need and you should not use it with new Date() because it will not work in safari

Lk77
  • 2,203
  • 1
  • 10
  • 15
  • So you mean if I replace below it should work. ```const formattedStartDate = new Date(format(parseISO(startDateInput), 'MM-dd-yyyy')); const formattedStartDate = new Date(format(fixDateString(course.session.startDate), 'MM-dd-yyyy')) ``` It will work for all 3 browsers. Right? – SMJ May 05 '22 at 12:45
  • yes it should work – Lk77 May 05 '22 at 13:27
  • What "*ISO 8601 bug in safari*"? The format 'MM-dd-yyyy' is not supported by ECMA-262 so parsing is implementation dependent. It is not an ISO 8601 format. – RobG May 05 '22 at 14:18
  • ```const formattedStartDate = new Date(format(fixDateString(course.session.startDate), 'MM-dd-yyyy'))``` this is not working, getting same invalid date. – SMJ May 05 '22 at 14:44
  • ISO 8601 format is like : 2022-05-05T14:25:45Z and safari does not support it, doing Date.parse('2022-05-05T14:25:45Z') will work in chrome and firefox but not safari, and it's the format the op has in his startDateInput. Try to console.log what the format function return – Lk77 May 05 '22 at 14:46
  • I'm looking at the date-fns doc and it seems it expect at date like so : `var result = format( new Date(2014, 1, 11), 'MM/DD/YYYY' )`, i don't know if passing a string should work – Lk77 May 05 '22 at 14:52
  • @lk77—the statement "*safari does not support it*" is simply wrong. Safari has supported that ISO 8601 format since long before it was required by ECMA-262 (about a decade ago). `new Date('2022-05-05T14:25:45Z')` works fine. – RobG May 06 '22 at 03:57
  • Perhaps it supports it but it's not reliable, i encountered the issue myself and was forced to borrow a mac to test it out, and it was not working ence the fix i was forced to make. Things like 2022-05-06T05:19:40+0000 will not work, at least in my testings, perhaps thay fixed it – Lk77 May 06 '22 at 05:57