John Snow
John Snow

Reputation: 5334

Very strange error involving a simple substring

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)

enter image description here

And heres output with number 880413 (again forget about the last 4 digits) enter image description here

Upvotes: 3

Views: 819

Answers (5)

Ivan Koblik
Ivan Koblik

Reputation: 4315

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.

Upvotes: 0

abuduba
abuduba

Reputation: 5042

Regular expression may help.

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

Upvotes: 0

NedStarkOfWinterfell
NedStarkOfWinterfell

Reputation: 5153

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));

Upvotes: 1

Bubbles
Bubbles

Reputation: 3815

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.

Upvotes: 3

AlanFoster
AlanFoster

Reputation: 8306

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));

Upvotes: 7

Related Questions