How to convert a String to long in javascript?

JavascriptStringLong IntegerUnix Timestamp

Javascript Problem Overview


I have a millisecond timestamp that I need to convert from a string to long. JavaScript has a parseInt but not a parseLong. So how can I do this?

To expand on my question slightly: given that apparently JavaScript doesn't have a long type, how can I do simple arithmetic with longs that are initially expressed as strings? E.g subtract one from the other to get a time delta?

Javascript Solutions


Solution 1 - Javascript

JavaScript has a Number type which is a 64 bit floating point number*.

If you're looking to convert a string to a number, use

  1. either parseInt or parseFloat. If using parseInt, I'd recommend always passing the radix too.
  2. use the Unary + operator e.g. +"123456"
  3. use the Number constructor e.g. var n = Number("12343")

*there are situations where the number will internally be held as an integer.

Solution 2 - Javascript

BigInt("1274836528318476135")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRichard HView Question on Stackoverflow
Solution 1 - JavascriptRuss CamView Answer on Stackoverflow
Solution 2 - JavascriptJameyView Answer on Stackoverflow