0

EDIT: One of my coworkers just mentioned that the way IE handles toLocalDateString() is different than other browsers... Looks like I'll be using moment.js to handle this issue. Also, this question is a duplicate, not sure if I should delete it or not. Here's a great answer:

ToLocaleDateString() changes in IE11

I am seeing some strange behavior while making performing a GET request with angular. I am only seeing this issue in IE11; both Chrome and Firefox are fine. I am sending two dates via query string parameters. I am appending the string as follows:

import { Http, Response, RequestOptions, Headers } from '@angular/http';
....

var date1 = new Date();
var date2 = new Date();
var start = date1.toLocaleDateString();
var end = date2.toLocaleDateString();
var url = '/localhost/someEndPoint?start=' +
        start +
        '&end=' +
        end +
        '&value1=' +
        data.value1 +
        '&value2=' +
        data.value2;
return this.http.get(url).
        map((response: Response) => <any>response.json())
        .publishReplay(1)
        .refCount()
        .catch(this.handleError);

When I check my network, I get strange characters in the url (not sure if they will show up here or not)

Request URL: http://localhost:37424/someEndPoint?start=â10â/â9â/â2018&end=â10â/â9â/â2018&value1=-1&value2=-1

If I were to hardcode those dates, the request goes through just fine. What's even more strange to me is that it will also work if I do (no hardcoding):

encodeURI(url);

I am sure I'm missing something, but things seem to point to IE doing something funky.

sparkyShorts
  • 630
  • 9
  • 28

2 Answers2

0

can you try javascript encodeURIComponent(url);

https://www.w3schools.com/jsref/jsref_encodeURIComponent.asp

Vivek Kumar
  • 4,822
  • 8
  • 51
  • 85
  • As I said in my post, encodeURI works just fine, so I'm sure your answer would do the same. I"m interested in the root cause of the issue, which issue is originating from IE treating toLocalDateString differently than other browsers. – sparkyShorts Oct 09 '18 at 21:22
  • 1
    IE is always mysterious, please don't take it seriously – Vivek Kumar Oct 09 '18 at 21:23
0

It is an IE 11 public behavior/bug (only happening in IE11). When using the toLocaleDateString method to get the date, it will add these Unicode characters \u200E (Left to right mark). To prevent this behavior, you could use the string.replace() method to remove these characters. Code as below:

var start = date1.toLocaleDateString().replace(/[\u200E]/g, "")
Zhi Lv
  • 18,845
  • 1
  • 19
  • 30