-1

I have a date that I am converting to LocaleDateString and then splitting it into an array inside an angulrajs controller. When I try to convert to int the elements in the array I get NaN. The characters in the array are numbers but the parse is not working.

How can I parse that data properly?

Code:

var dateLocal = $scope.startDate.toLocaleDateString(); //Has this ‎6‎/‎5‎/‎2015
var dateSplitted = dateLocal.split("/"); //Has [6,5,2015]
var month = parseInt(dateSplitted[0]);  //HERE If I use parseIntI get NaN originally it has 6
var day = dateSplitted[1];//Has 5
var year = dateSplitted[2]; Has 2015

I want to be aple to convert to string month day and year.

harmic
  • 28,606
  • 5
  • 67
  • 91
Devsined
  • 3,363
  • 6
  • 30
  • 48

3 Answers3

4

You rely on toLocaleDateString, which is implementation dependent:

This function returns a String value. The contents of the String are implementation-dependent

The problem is that your browser returns a string with some left-to-right marks (U+200E).

See the difference:

var str1 = "6", // "\u0036"             <-- OK
    str2 = "‎6‎"; // "\u200e\u0036\u200e" <-- Your "corrupted" string
parseInt(str1); // 6
parseInt(str2); // NaN

So you shouldn't trust the value returned by that method. Instead, use date methods to get the day, month and year.

Oriol
  • 274,082
  • 63
  • 437
  • 513
  • 1
    And parseInt returns NaN if the first character it finds is not a digit. So you have to trim non-digits with a regex. – Domino Jun 21 '15 at 02:07
  • @Oriol How did you know it was left-to-right marks? – tavnab Jun 21 '15 at 02:11
  • @tavnab OP says "*Has this ‎6‎/‎5‎/‎2015*". This string contains those U+200E. – Oriol Jun 21 '15 at 02:14
  • @Oriol I'm sorry, but I don't see it in the OP's sample code/comments, and inspecting this page's source doesn't show anything but "6/5/2015" (with no RTL marks) in the HTML. Can you tell me how you noticed the 2 characters? – tavnab Jun 21 '15 at 02:21
  • @tavnab Browsers display the character, so paradoxically it can't be seen. Copy that text and paste it in a text editor and you will see it. – Oriol Jun 21 '15 at 02:24
  • 1
    Thanks @Oriol; for future readers, apparently this is a [bug reported for IE11](https://connect.microsoft.com/IE/feedback/details/811107/ie-11-javascript-date-tolocaledatestring-returns-a-string-that-contains-left-to-right-mark-characters-which-may-not-be-used-to-create-a-new-date) – tavnab Jun 21 '15 at 02:25
  • The issue of using get day is I am getting the days and months with the time zone offset and then was painful just to compare my date with other date in UTC – Devsined Jun 21 '15 at 03:52
  • If I try $scope.startDate.getDay() and $scope.startDate.getMonth() I get 5 and 5 using 6/5/2015. There there is rabit whole on how the datepicker of angularui is working with javascript date. – Devsined Jun 21 '15 at 03:58
  • @Devsined If you want UTC, there are `getUTCDate`, `getUTCMonth` and `getUTCFullYear`. – Oriol Jun 21 '15 at 14:01
1

This doesn't answer why you are getting NaN in your code, but instead of converting the date object to a string and parsing the parts, you can get the parts of the date directly using Date.prototype.getMonth(), Date.prototype.getDate() (day-of-month), and Date.prototype.getFullYear().

tavnab
  • 2,594
  • 1
  • 19
  • 26
  • The issue with this oslution is I am getting the offset of the timezone and later I cannot compare with other dates – Devsined Jun 21 '15 at 04:03
  • 1
    Apologies, it's not clear why using these functions won't work from your example. The result of calling the above functions on `$scope.startDate` is equivalent to getting the string & parsing it, but shorter. If you also need to extract the timezone, you can use [`Date.prototype.getTimezoneOffset()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset). Can you please clarify if I'm misunderstanding your use-case? – tavnab Jun 21 '15 at 04:09
0

I tried out your code, and it seemed to work fine for me. For your dateLocal, I just replaced your value with var dateLocal = new Date().toLocaleDateString(); because I didn't know what value was loaded from your scope. When I did this, the code loaded fine, so you may want to double check the nature of the variable you are loading from the scope.