new Date('2020-08-18 07:52') is working in Chrome, it returned
Tue Aug 18 2020 07:52:00 GMT+0800 (Malaysia Time)
but safari gave me invalid date? what's the best way to fix this? this bug in safari is breaking my entire app.
new Date('2020-08-18 07:52') is working in Chrome, it returned
Tue Aug 18 2020 07:52:00 GMT+0800 (Malaysia Time)
but safari gave me invalid date? what's the best way to fix this? this bug in safari is breaking my entire app.
without need for tools/plugins/packages, I would simply separate the received date:
var apiDate = '2020-08-18 07:52'
var [ date, time ] = apiDate.split(' ')
var [ year, month, day ] = date.split('-')
var [ hour, minute ] = time.split(':')
var newDate = new Date(year, month - 1, day, hour, minute, 0)
// Tue Aug 18 2020 07:52:00
I would just be careful about TimeZones, as your date has no time zone description ...
Note from the code above, one might think that subtracting 1 from a string is invalid, but that's the caveats of javascript...
"08" + 1 = "081"
"08" - 1 = 7
if the format is always the same, you can also do:
var apiDate = '2020-08-18 07:52'
var newDate = new Date(`${apiDate.replace(' ', 'T')}:00`) // '2020-08-18T07:52:00'
// Tue Aug 18 2020 07:52:00