Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does parsing a c# date to be a javascript date even work with the ')'?

Tags:

javascript

c#

I know that to convert a C# date, like /Date(1430341152570)/ to a JavaScript date, it's the following:

var part = "/Date(1430341152570)/".substr(6); // => "1430341152570)/"
var jsDate = new Date(parseInt(part));

My question: how is the part value, which includes a trailing )/ able to be parsed into an int via parseInt? Wouldn't JS throw an error on trying to convert something that has the )/ characters? It would make more sense to me if the part value was something like "/Date(......)/".substr(6).replace(')/','') b/c at least you're making it a string of #'s to be parsed to an int.

like image 395
Ian Davis Avatar asked Jan 09 '23 11:01

Ian Davis


1 Answers

From the Mozilla documentation:

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.

) isn't a numeral, so everything from there onwards is ignored as specified.

Or from the ES6 draft standard:

parseInt may interpret only a leading portion of string as an integer value; it ignores any code units that cannot be interpreted as part of the notation of an integer, and no indication is given that any such code units were ignored.

like image 151
Jon Skeet Avatar answered Jan 20 '23 15:01

Jon Skeet