1

I'm having a service method and it returns the DateTime object, but its failed to parse it in Internet Explorer.

I'm getting the sample service response

enter image description here

I would like to do operatinons like .getDate(), .getHour(), etc.,

My Piece of Code:

dateOperation(data: Date): any {
    console.log(data);
    console.log(data.getMinutes());
}

I'm getting the following console error in Internet Explorer.

enter image description here

If I'm doing direct console.log(data);, it prints the date as 2018-06-27 05:16:01.00 +00:00 - But I can't able to do any apply any date related functions. Kindly assist me how to handle datetime in Internet Explorer browser ? Rest of the browser it works very fine.

I'm using Internet Explorer 11.

enter image description here

Sreedharan
  • 83
  • 1
  • 3
  • 8
  • You should post data as text, not images because 1. not everyone can see the images and 2. images of text are very much more difficult to work with. – RobG Jun 27 '18 at 23:38
  • The date string in the object is not consistent with ISO 8601 or ECMA-262, so parsing is implementation dependent. They may return an invalid date because of the incorrect format, or might parse it in any way that seems reasonable. I expect Safari may also return an invalid date. – RobG Jun 27 '18 at 23:40
  • @RobG - I'm having issues only in Internet Explorer, it's works very fine in Mozilla Firefox, Chrome even Edge too.. Moreover it's not duplicate question, My question speaks about the Browser related issue while on parsing, the question marked by you speaks about common parse issue. First try to understand the scope of question. – Sreedharan Jun 28 '18 at 05:16
  • @RobG - I already posted the date as text `2018-06-27 05:16:01.00 +00:00`, kindly refer the question once again thoroughly. – Sreedharan Jun 28 '18 at 05:17
  • I've read the OP very thoroughly and I can't see the relevant part of the JSON posted as text anywhere (yes, you posted the date string itself). This is an exact duplicate because as you've discovered, parsing of non–standard Date strings is implementation dependent, that's why you get different results in different browsers (as noted in my earlier comment). – RobG Jun 29 '18 at 02:54

1 Answers1

1

I use the following new Date constructor for IE

 new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);

so in your case just split input string on any non–digit as RobG suggested

 var dateString = "2018-13-27 05:16:01,00";
 var dateArray = date.split(/\D/); //["2018","12","27","05","16","01","00"]

 var date = new Date(Date.UTC(dateArray[0], dateArray[1]-1, dateArray[2], dateArray[3], dateArray[4], dateArray[5], dateArray[6]));

looks bit dirty but it works in IE =)

Rudolf Manusachi
  • 2,311
  • 2
  • 19
  • 25
  • 1
    *replace* isn't needed, you can just use `split(/\D/)` to split on any non–digit. ;-) The OP's string also contains a timezone offset, which may not always be zero so you might need to account for that too. – RobG Jun 27 '18 at 23:41
  • @RobG super, thanks! – Rudolf Manusachi Jun 27 '18 at 23:45