I am trying to convert js format date to string and then to json format.
The date input I get from my model is "/Date(1511532984927)"
Then I am converting it (variable values in a comment after the variable):
var stringDate = dateString.replace(/\/Date[\(]/, "").replace(/[\)]/, "").replace(/[\/]/, ""); // "1511532984927"
var intDate = parseInt(stringDate); // 1511532984927
var dateDate = new Date(intDate); // Fri Nov 24 2017 14:16:24 GMT+0000 (GMT Standard Time)
var localDate = dateDate.toLocaleDateString(); // "24/11/2017"
var len = localDate.length; // 15
All values come the same in Chrome, Firefox and IE except that in IE the length comes as 15 not 10.
So if I'm trying to convert the resulted string (24/11/2017) back to Date() I'm getting incorrect component strings (day, month, year) in IE and the resulted Date comes invalid:
var day = dateString.substr(0, 2); // FF, Chrome: 24 - IE: 2
var month = dateString.substr(3, 2); // FF, Chrome: 11 - IE: /
var year = dateString.substr(6, 4); // FF, Chrome: 2017 - IE: 11/
new Date(month + ' ' + day + ' ' + year);
To get the correct date components is IE I need to get them like this:
var test = dateString.substring(1, 3);
var test2 = dateString.substring(5,8);
var test3 = dateString.substring(10);
How come a string "24/11/2017" can give length of 15 characters in IE?
Why is it counting index of each character in a different way?
I am using IE11 on Windows 8.1 on Apple bootcamp
** EDIT **
I think I didn't explain it well what I'm doing so it caused confusion.
I have a string "24/11/2017" and need to convert it to js Date() object.
I'm no necessary doing it from string produced with toLocalDateString(), it was only another example.
Thanks but all replies so far are irrelevant to what I need doing.