3

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

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). Sep 10, 2012 at 20:16

5 Answers 5

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));
4
  • Number() doesn't take a second argument. Sep 10, 2012 at 20:19
  • This seems to be working. Thanks for providning me with the correct answer and explaining to make things more clear! (cannot accept yet, will accept in 5 mins:) )
    – John Snow
    Sep 10, 2012 at 20:19
  • @ŠimeVidas Haha yeah sorry. That's what I get for copypasting and changing the function :)
    – AlanFoster
    Sep 10, 2012 at 20:19
  • @JimmyGustafsson No problem :) Douglas Crockford covers examples like this in his talks and books; Be sure to check those out :)
    – AlanFoster
    Sep 10, 2012 at 20:24
3

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.

1

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

Regular expression may help.

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

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.

4
  • The question is JavaScript... not Java. Unless you're suggesting the use of an external library?
    – AlanFoster
    Sep 10, 2012 at 20:20
  • Yeap I was thinking about Java, this question is tagged incorrectly. To remedy my mistake OP can try this. Sep 10, 2012 at 20:23
  • He can't try your example, JavaScript does not offer a 'printf' function natively :(
    – AlanFoster
    Sep 10, 2012 at 20:25
  • Thanks for noticing it, I have modified my response. Sep 10, 2012 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.