Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8507795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:58:14+00:00 2026-06-11T02:58:14+00:00

I am trying to make a function which are supposed to check if a

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

enter image description here

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:58:16+00:00Added an answer on June 11, 2026 at 2:58 am

    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));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make it so I can write a function which defines a
trying to make a page which will recursively call a function until a limit
Im trying to make a function, that takes an input, determines its value, and
I am trying to make a timestamp function that checks which displays a time
I'm trying to make a function array, which I'm using inside a List.reduce HOF.
I'm trying to make function primes which is a list of prime numbers, but
I am trying to make a function which can print a polynomial of order
I am trying to make a function which converts a mysql query result in
I'm currently trying to make a set of conversion functions which, through one call,
Im trying to make a function that will return an element of type point:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.