2013-03-10 72 views
0
var i = 20040115102010000; 

i++; 

returns 20040115102010000; 

我必須使用大號碼庫嗎?爲什麼我不能在javascript中添加1個大數字

What is the standard solution in Javascript for handling big numbers (BigNum)?

這個數字已經在浮點格式和我感動的小數位的左三圈。如果你的速度很快,你會注意到它是一個日期。我會先把這個數字轉換成日期格式嗎?我會發現在Date()對象中以毫秒爲單位增加更容易嗎?

+0

我不想聽起來居高臨下的'如果你很快',但我確實需要指出,這確實是一個日期。 – TMB 2013-03-10 02:43:29

+3

請參閱http://stackoverflow.com/questions/307179/what-is-javascripts-max-int-whats-the-highest-integer-value-a-number-can-go-t。 – bfavaretto 2013-03-10 02:44:50

+2

http://www.thefreedictionary.com/behove – TMB 2013-03-10 02:57:53

回答

2

您不能在javascript中使用大的日期,而無需使用bigDay庫來處理您的數字。

/*

從 'https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date':

自午夜01月01日的javascript日期以毫秒爲單位,1970年UTC。一天持有86,400,000毫秒。相對於1970年1月1日UTC,JavaScript Date對象範圍爲-100,000,000天至100,000,000天。 */

var firstday=new Date(1970,0,1),lastday=new Date(1969,11,31); 

firstday.setDate(firstday.getDate()-100000000); 

lastday.setDate(lastday.getDate()+100000000); 

firstday.toUTCString()+'; timestamp: '+firstday.getTime()+'\n'+ 
lastday.toUTCString()+'; timestamp: '+lastday.getTime(); 

/*返回值:(JS中的最大和最小日期)

週二,4月20日04:00:00 -271821 GMT;時間戳:-8639999985600000

9月12日星期五275760 04:00:00 GMT;時間戳:8639999928000000 */

相關問題