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

  • SEARCH
  • Home
  • 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 6071455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:02:57+00:00 2026-05-23T10:02:57+00:00

This is very weird I don’t know what I’m doing wrong. I have a

  • 0

This is very weird I don’t know what I’m doing wrong. I have a function to grab the date (i.e in this format: 06/24/2011), here’s the function:

function checkDate(input){
    var d = new Date();
    var dspl = input.split("/");

    if(dspl.length != 3)
        return NaN;

    d.setDate(dspl[1]);

    d.setMonth(Number(dspl[0])-1);

    if(dspl[2].length == 2)
        d.setYear("20"+(dspl[2]+""));
    else if(dspl[2].length == 4)
        d.setYear(dspl[2]);
    else
        return NaN;

    var dt = jsToMsDate(new Date(d));
    return dt;
}

If I enter any date of the month, it would parse the date correctly, but if I enter 31st, i.e "01/31/2011", then it would turn into "01/01/2011". I’m not sure what to do and not really sure where the problem might be.

  • 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-05-23T10:02:58+00:00Added an answer on May 23, 2026 at 10:02 am

    JavaScript’s Date objects allow you to give invalid combinations of months and days; they automagically correct those for you (so for instance, if you set the day of the month to 31 when the month is June, it automatically makes it July 1st). That means if you set the fields individually, you can run into situations where that automagic correction gets in your way.

    In your case, if you’re going to set all three of those fields, you’re better off using the form of the Date constructor that accepts them as arguments:

    var dt = new Date(year, month, day);
    

    (If you want hours, minutes, seconds, and milliseconds, you can add them as parameters as well.)

    So looking at your code, an off-the-cuff update:

    function checkDate(input){
        var year, month, day, d, dt;
        var dspl = input.split("/");
    
        if(dspl.length != 3)
            return NaN;
    
        year  = parseInt(dspl[2], 10);
        month = parseInt(dspl[0], 10) - 1;
        day   = parseInt(dspl[1], 10);
        if (isNaN(year) || isNaN(month) || isNaN(day)) {
            return NaN;
        }
    
        if (year < 100) {
            year += 2000;
        }
    
        d = new Date(year, month, day);
    
        var dt = jsToMsDate(d);
        return dt;
    }
    

    Some other notes on that update:

    • It’s best to use parseInt to parse numbers from end users, and to always specify the radix (10 for decimal). (No, parseInt is not slower than Number or the unary + trick. People assume it is, but it isn’t.)
    • No need to muck about with strings to add 2000 to years given with only two digits. But you can if you like. Note I weakened the validation there, allowing one-digit years for (say) 2001 and three-digit years for (say) 300 AD. So if you need it to be that strong, you’ll need to readjust that.
    • No need to feed the date instance into new Date() again.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All right, this one is going to sound very weird and I don't know
This is a very weird problem. I think I must be doing something wrong,
This is very weird. I have the following code: Assert.AreEqual(new DateTime(2000, 1, 1), DateTime.ParseExact(2000,
Ok so this bug is very weird here is my code, you can actually
So I have this weird looking problem: my very basic program generates an error
I have a really weird compilation problem with JNI and don't know how to
It is very weird, and I don't have any idea what is the issue!
Ok, this is very weird. I'm trying to do a database migration, and all
This is a very weird problem. My app that runs just fine but somehow
Given this very familiar model of prototypal construction: function Rectangle(w,h) { this.width = w;

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.