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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With