最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Very strange error involving a simple substring - Stack Overflow

programmeradmin3浏览0评论

I am trying to make a function which are supposed to check if a certain date is in the correct format. Do to this, the function takes in a variable, and breaks it down to three subvariables, year, month, and day. Later on I want to check each of these substrings against in a few if-statements.

When i run this with my GFs number which is 880413 then year=88, month=4, day=13 However, when I run this function with my own number which is 820922, then the function sets year to 82, month to 0?! and day=22.

How is this possibe? when month is 04, it cuts it off and shows only 4, but when month is 09 it cuts it off to 0.

Heres relevant code:

function isValidDate(date) 
{
    var valid = true;

    var year = parseInt(date.substring(0, 2));
    var month = parseInt(date.substring(2, 4)); // error here!
    var day = parseInt(date.substring(4, 6));
    alert(year+"--"+month+"--"+day+"--")
}

Heres output when running number 820922 (forget about the last 4 digits, they are for swedish social security number and do no need to be considered in this example)

And heres output with number 880413 (again forget about the last 4 digits)

I am trying to make a function which are supposed to check if a certain date is in the correct format. Do to this, the function takes in a variable, and breaks it down to three subvariables, year, month, and day. Later on I want to check each of these substrings against in a few if-statements.

When i run this with my GFs number which is 880413 then year=88, month=4, day=13 However, when I run this function with my own number which is 820922, then the function sets year to 82, month to 0?! and day=22.

How is this possibe? when month is 04, it cuts it off and shows only 4, but when month is 09 it cuts it off to 0.

Heres relevant code:

function isValidDate(date) 
{
    var valid = true;

    var year = parseInt(date.substring(0, 2));
    var month = parseInt(date.substring(2, 4)); // error here!
    var day = parseInt(date.substring(4, 6));
    alert(year+"--"+month+"--"+day+"--")
}

Heres output when running number 820922 (forget about the last 4 digits, they are for swedish social security number and do no need to be considered in this example)

And heres output with number 880413 (again forget about the last 4 digits)

Share Improve this question edited Sep 10, 2012 at 20:13 kosa 66.7k15 gold badges134 silver badges170 bronze badges asked Sep 10, 2012 at 20:11 John SnowJohn Snow 5,3545 gold badges38 silver badges44 bronze badges 1
  • parseInt('09') // => 0, but parseInt('09', 10) // => 9. If you don't provide the radix, the function assumes it's an octal value (since it starts with a zero), and as a result doesn't parse beyond the initial 0 (9 is not an octal digit). – Šime Vidas Commented Sep 10, 2012 at 20:16
Add a ment  | 

5 Answers 5

Reset to default 7

Always specify a radix when using parseInt()

The reason for this is if you don't specify it, it doesn't default to base 10, instead it guesses based on the first digit/character. IE if it starts with a '0' then it is intepreted as octal

function isValidDate(date) 
{
    var valid = true;

    var year = parseInt(date.substring(0, 2), 10);
    var month = parseInt(date.substring(2, 4), 10);
    var day = parseInt(date.substring(4, 6), 10);
    alert(year+"--"+month+"--"+day+"--")
}

Or use Number() ie

var month = Number(date.substring(2, 4));

Specify the base - use parseInt("09", 10). For legacy reasons, starting a number with a zero will be parsed as an octal. "8" and "9" are not valid octal digits, so "09" cannot be successfully parsed as an integer.

Edit - doing a bit of digging, and apparently what you did does fit the official ECMAscript standard, but most implementations use octal formatting anyway - details here.

You will see the same problem in case of 08 also. This is because Javascript treats numbers starting with 0 as octal, and there is no 09 or 08 in octal. So you can provide the base as the 2nd parameter:

alert(parseInt('08',10));

Regular expression may help.

date.replace( /(\d{2})(\d{2})(\d{2})/,'$1--$2--$3--');  

When you concatenate string with an integer it will never give you 04. To get the result that you want you need to use printf:

'%02d--%02d--%02d'.sprintf(year, month, day);

Function sprintf is from Prototype library.

发布评论

评论列表(0)

  1. 暂无评论