Javascript parseInt 给出了非常意想不到的结果
我正在解析一个字符串以检查它是否是一个日期,现在我们偶然发现我的方法不适用于八月或九月的日期。这就是我所做的(显然,输入并不是真正硬编码的,但为了简洁......):
var str = '2010-08-26 14:53';
var data = str.split(' '); // ['2010-08-26', '14:53']
var date = data[0].split('-'); // ['2010', '08', '26]
var time = data[1].split(':'); // ['14', '53']
var yyyy = parseInt(date[0]); // 2010
// THIS IS WHERE STRANGE THINGS HAPPEN:
var MM = parseInt(date[1]); // 0 - not 08 or 8, as expected!
console.log(date[1]); // prints "08" (with quotes)
console.log(date[1].toString()); // prints 08 (no quotes)
console.log(parseInt(date[1].toString())); // prints 0 (!)
这个问题在八月和九月以及每个月的 8 号和 9 号出现 -也就是说,当 "08"
或 "09"
被解析为整数时,返回 0
而不是 8或<代码>9。该代码适用于较低的(例如
"07"
)和较高的(例如 "10"
)整数(至少在预期的日期范围内......)
我在做什么错误的?
Possible Duplicate:
Workarounds for JavaScript parseInt octal bug
I'm parsing a string to check if it's a date, and by chance we now discovered that my method doesn't work for dates in august or september. This is what I do (the input isn't really hard-coded, obviously, but for brevity...):
var str = '2010-08-26 14:53';
var data = str.split(' '); // ['2010-08-26', '14:53']
var date = data[0].split('-'); // ['2010', '08', '26]
var time = data[1].split(':'); // ['14', '53']
var yyyy = parseInt(date[0]); // 2010
// THIS IS WHERE STRANGE THINGS HAPPEN:
var MM = parseInt(date[1]); // 0 - not 08 or 8, as expected!
console.log(date[1]); // prints "08" (with quotes)
console.log(date[1].toString()); // prints 08 (no quotes)
console.log(parseInt(date[1].toString())); // prints 0 (!)
This problem arises for august and september, and for the 8th and 9th every month - that is, when either "08"
or "09"
is being parsed to integer, 0
is returned instead of 8
or 9
. The code works for both lower (e.g. "07"
) and higher (e.g. "10"
) integers (at least within expected date ranges...)
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
用于
确保字符串被解释为以 10 为基数(十进制)。
否则,如果以
"0"
开头,则解释为基数 8(八进制);如果以"0x"
开头,则解释为基数 16(十六进制)。事实上,您应该始终包含基本参数以避免此类错误。
Use
to make sure the string is interpreted as base 10 (decimal).
Otherwise, it is interpreted as base 8 (octal) if it starts with
"0"
, or as base 16 (hexadecimal) if it starts with"0x"
.In fact, you should always include the base argument to avoid these kinds of bugs.
这是因为以 0 开头的数字被视为八进制数字,而“08”不是八进制,因此返回 0。
阅读此内容:http://mir.aculo。 us/2010/05/12/adventures-in-javascript-number-parsing/
That's because numbers that start with 0 are treated as octal numbers and "08" is not an octal so 0 is returned.
Read this: http://mir.aculo.us/2010/05/12/adventures-in-javascript-number-parsing/
parseInt() 函数有第二个可选基数参数。如果省略 radix 参数,JavaScript 会进行以下假设:
因此在您的情况下,它假定为八进制数。更改为 var MM = parseInt(date[1], 10); 就可以了
The parseInt() function has the second optional radix parameter. If the radix parameter is omitted, JavaScript assumes the following:
So in your case it assumes the octal numbers. Change to
var MM = parseInt(date[1], 10);
and it will work听起来你的日期被解析为十六进制 - 前导零正在执行此操作。
因为 0-7 的十六进制和十进制表示形式是相同的,所以它“有效”,但 8 和 9 的转换不正确。
使用
显式指定基数。
It sounds like your date is being parsed as hex - the leading zero is doing this.
Because the hex and decimal representations are the same for 0-7 it 'works', but 8 and 9 get incorrectly converted.
Use
To specify the base explicitly.