Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a String to long in javascript?

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?

Thanks

Edit: 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?

like image 365
Richard H Avatar asked Mar 27 '11 14:03

Richard H


People also ask

Can you convert a string to a long?

We can convert String to long in java using Long. parseLong() method.

Can we convert string to long in Java?

There are many methods for converting a String to a Long data type in Java which are as follows: Using the parseLong() method of the Long class. Using valueOf() method of long class. Using constructor of Long class.

How do I convert a string to a number in JavaScript?

How to convert a string to a number in JavaScript using the parseInt() function. Another way to convert a string into a number is to use the parseInt() function. This function takes in a string and an optional radix. A radix is a number between 2 and 36 which represents the base in a numeral system.

How do I convert a string to an array in JavaScript?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.


1 Answers

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.

like image 164
Russ Cam Avatar answered Sep 21 '22 23:09

Russ Cam