0

I receive this value (2018-12-19T14:07:03.068+0000) from a service of BE, try to transform in date

new Date('2018-12-19T14:07:03.068+0000')

This works in Chrome, but IE outputs "Invalid Date"

Cœur
  • 37,241
  • 25
  • 195
  • 267
Phil
  • 43
  • 1
  • 6

2 Answers2

0

Try to use a date with RFC2822 or ISO 8601 format.

Community
  • 1
  • 1
Sarah117
  • 91
  • 1
  • 7
  • this is fine, but I receive this value from a service of BE – Phil Dec 20 '18 at 08:27
  • You should not rely on RFC2822 as it is not a supported date time format according to the ECMAScript specification. – str Dec 22 '18 at 15:16
0

It fails because it is not a valid date time string. The best option would be to fix it in the backend. If you can't do that, fix it in the frontend. For example as follows:

var invalidDate = '2018-12-19T14:07:03.068+0000';
var validDate = invalidDate.substring(0, 26) + ':' + invalidDate.substring(26);
console.log(validDate, new Date(validDate));
str
  • 42,689
  • 17
  • 109
  • 127