How to convert a String to long in javascript?

前端 未结 2 1560
难免孤独
难免孤独 2020-12-01 02:21

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?

2条回答
  •  长情又很酷
    2020-12-01 02:55

    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.

提交回复
热议问题