最近用js作了个日历,其中用到了parseInt方法,在月份转换中出了点问题,在此记录下:
比如 八月份 "08" 用parseInt("08") 得到的结果是 1;
搜了下parseInt的用法,才知道问题原因:
Definition and Usage
定义与用法
The parseInt() function parses a string and returns an integer.
parseInt()方法用于将一个字符串转换为整型数字。
The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number.
进制参数用于指定目标字符串的进制类型。如:设定进制为16,目标字符串将从16进制先转为10进制再进行处理。
If the radix parameter is omitted, JavaScript assumes the following:
如果没有指定进制参数,JavaScript将依照下列规则进行:
- If the string begins with "0x", the radix is 16 (hexadecimal)
如果字符串以"0x"开始,视为16进制 - If the string begins with "0", the radix is 8 (octal). This feature is deprecated
如果字符串以"0"开始,视为8进制 - If the string begins with any other value, the radix is 10 (decimal)
其他的视为10进制
Syntax
语法
parseInt(string, radix) |
Parameter 参数 | Description 注释 |
---|---|
string 字符串 | Required. The string to be parsed 必选项。所要转换的字符串 |
radix 进制 | Optional. A number (from 2 to 36) that represents the numeral system to be used 可选项。2到36,指定字符串的进制类型 |