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?
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