0

In I.E. 11, in the console window, if I type new Date().toLocaleString(), I get something like "2/4/2016 9:12:05 AM". However, if I add .length, I get 32. The string is 19 "readable" characters, so what's up with the 32 and is there an option I can invoke that will give me a string of length 19?

If I type new Date(new Date().toLocaleString()), I get [date] Invalid Date, whereas if I type new Date(new Date("2/4/2016 9:12:05 AM")) I get a legitimate date.

My locale is "en-US".

Kelly Cline
  • 2,206
  • 2
  • 33
  • 57

3 Answers3

0

You are taking the lenght of the whole string. In this case what this function return is : Thu Feb 04 2016 17:28:09 GMT+0200 (FLE Standard Time) <-- 32 chars. Try to get the new Date as a variable and use it.

var example = new Date();

  • What the function returns in your locale is not necessarily the same as what it returns in the locale of the OP. – Pointy Feb 04 '16 at 15:40
  • Correct, But the format will be always the same. *if there are no aditional methods new Date (); will return your local time and date in this format "Thu Feb 04 2016 17:28:09 GMT+0200" – Mladen Mladenov Feb 04 '16 at 15:41
  • The issue is not what new Date() returns, but what toLocaleString() returns. – Kelly Cline Feb 04 '16 at 15:43
  • I think the whole point of the question is that the OP is seeing something that is *not* the same. – Pointy Feb 04 '16 at 15:43
0

This happens with IE11 as well. I have encountered same issue, fixed it by using below date, this way you do not see any invisible empty character.

var cleanDate = (new Date()).toISOString();

Here is solution for specific to your problem, if you do not want to use above method of getting date.

//Custom extension method to replace all found value.
String.prototype.replaceAll = function(find, replace) {
var target = this;
    return target.split(find).join(replace);
};
//Find there is invisible empty character
var emptyCode = (new Date()).toLocaleString().charCodeAt(0);
var cleanDate = undefined;
if(emptyCode === 8206)
{
    //Remove all invisiable empty characters
    cleanDate =(new Date()).toLocaleString().replaceAll(String.fromCharCode(emptyCode),'');
}

Extension Method can be found from below post. How to replace all occurrences of a string in JavaScript?

Community
  • 1
  • 1
Hi10
  • 531
  • 1
  • 5
  • 21
0

maybe this solution can help (in dd.mm.yyyy format)

var curDate = new Date().toLocaleString().split(',')[0];
Idan
  • 3,604
  • 1
  • 28
  • 33