0

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.

nickornotto
  • 1,946
  • 4
  • 36
  • 68

2 Answers2

0

There is an existing question already regargin the different behaviour in browser for locale transformations. Link is here.

You are converting your string to a Date object. Why can't you use the date functions to get the month, day and year?

var stringDate = dateString.replace(/\/Date[\(]/, "").replace(/[\)]/, "").replace(/[\/]/, ""); // "1511532984927"
var intDate = parseInt(stringDate); // 1511532984927
var dateDate = new Date(intDate);
var month = dateDate.getMonth() // 10
var day = dateDate.getDate() // 24
var year = dateDate.getFullYear() // 2017
console.log(day, month, year);
// 24 10 2017

month is zero based. more info here

and create again a new object with `var myNewDate = new Date(year, month, day);

ztadic91
  • 2,774
  • 1
  • 15
  • 21
  • I am using new Date(intString) to convert to new date and it works, that's not an issue, just wanted to demonstrate the date string length is wrong in this case. The problem is when I'm trying to convert from string date eg. 24/11/2017 into new Date() using substr or substring functions. – nickornotto Nov 24 '17 at 15:51
  • as mentioned each browser has it's own implemenation for the function. I've run your snippet in chrome and got `"11/24/2017"` and got in IE `"‎24‎.‎11‎.‎2017‎."` Basically I am trying to say, that you shouldn't rely on the function as it isn;t consistent throughout browsers – ztadic91 Nov 24 '17 at 15:57
0

So if I'm trying to convert the resulted string (‎24‎/‎11‎/‎2017) back to Date() I'm getting incorrect component

Don't do that.

The output of toLocaleString is entirely implementation dependent, so it may be different in different hosts and on different systems. I get different results in Safari, Chrome and IE (which are different to yours because my system settings are different).

Further, implementations are not required to correctly parse their own output (which they are required to do for their output from Date.prototype.toString).

How come a string "‎24‎/‎11‎/‎2017" can give length of 15 characters in IE?

I think you mean "how can I make the format produced by IE the same as others". The answer is to not use toLocaleString and format it yourself (use Date methods or a library). But both methods are extremely unreliable, there is no single format for toLocaleString, so other browsers on other hosts in different regions using different languages will produce different results.

You absolutely can't rely on it to be consistent, even in one browser. It is supposed to produce different results depending on the host environment.

Why is it counting index of each character in a different way?

No idea, but your fundamental issue is expecting a consistent output from toLocaleString, which it is not designed to do by specification.

So you options are:

  1. Keep a reference to the original Date or the string that produced it
  2. Use toString instead, which is still inplementation dependent but browsers are required to parse their own format (but in some cases browsers will still get it wrong)
  3. Use an unambiguous format that you can parse reliably (i.e. not using the built-in parser), e.g. D-MMM-YYYY

Option 1 is by far the best.

PS.

You can parse the string to a Date much more simply by just removing all the non–digits:

var s = '/Date(1511532984927)';
var d = new Date(+s.replace(/\D/g,''));

console.log(d.toLocaleString());
RobG
  • 142,382
  • 31
  • 172
  • 209