0

I am currently making a series of REST calls to a backend API and I have no control over the format of the date being sent back in the JSON.

The format that is being sent is this

Wed, 21 Nov 2018 03:00:00.000Z

IE11 considers this an invalid date. I have been using moment.js to get the current date and time and comparing it to the date and time being sent in the API. It is working perfectly everywhere except in IE. I have been trying everything I can from the Moment docs but everything that I return is considered invalid by IE11.

I am setting my date as follows

var date = new Date("Wed, 21 Nov 2018 03:00:00.000Z");  

Update: I have also tried setting the date using moment

var date = "Wed, 21 Nov 2018 03:00:00.000Z"
date = moment(d, "YYYY-MM-DD HH:mm:ss").toDate();

I have tried many different formats and everything returns invalid.

This is what returns as Invalid according to IE. I have tried converting the date to a moment object first and then into a valid date format but that has not seemed to work either.

I was able to conclude that IE does not like the .000Z at the end of the date. It works if I cut that off but that all my times are in GMT.

steveg152
  • 11
  • 4
  • Possible duplicate of [Parse string to date with moment.js](https://stackoverflow.com/questions/22184747/parse-string-to-date-with-moment-js) – Liam Nov 16 '18 at 15:21
  • 2
    Show how you tried in moment. You probably forgot to set format argument properly – charlietfl Nov 16 '18 at 15:21
  • 1
    If you know the format, not too hard to parse it into a format you know all browsers support. – epascarello Nov 16 '18 at 15:21
  • 1
    don't parse the date (`new Date`) yourself, let moment do it (`moment(str, 'format').toDate();`) – Liam Nov 16 '18 at 15:21
  • @Liam I am using `let d = "Wed, 21 Nov 2018 03:00:00.000Z" d = moment(d, "YYYY-MM-DD HH:mm:ss").toDate();` Every format that I use is not working. It is just returning Invalid Date, even in Chrome. I have tried using moment to parse the date in many different ways and nothing has worked. Maybe I am doing something wrong with moment. – steveg152 Nov 16 '18 at 16:57

1 Answers1

1

The format YYYY-MM-DD HH:mm:ss you're putting in your momentJS constructor bears no resemblance to the date string you're actually inputting...you're telling moment to expect something like "2018-11-16 17:10:02". Maybe you've confused this with the format you want to output later, I'm not sure, because it clearly doesn't even come close to matching the example data.

Check http://momentjs.com/docs/#/parsing/string-format/ and choose the appropriate tokens to match the date format you're providing. Here's an example which will work for the date given in the example:

var d = "Wed, 21 Nov 2018 03:00:00.000Z";
var m = moment(d, "ddd, DD MMM YYYY HH:mm:ss.SZ"); //parse the date based on the format tokens given
console.log(m.format("YYYY-MM-DD HH:mm:ss")); //output the date in a different format
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • This worked perfectly. My mistake seems to have been with the format that I was trying to use. Thanks for your help. – steveg152 Nov 16 '18 at 21:27