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 8368441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:23:26+00:00 2026-06-09T13:23:26+00:00

Well I know this question is asked a thousand times on internet but I

  • 0

Well I know this question is asked a thousand times on internet but I am a rookie and don’t know much about Javascript functions. I want to compare two date.
Input is in form = “2011-jan-21”

Here is what I have come up, Please help! I will really appreciate because I need to get it worked urgently.

   function compareDates() {
            var startDate=document.getElementById("startDate").value;
            var endDate = document.getElementById("endDate").value;
                months = {'jan': '01', 'feb': '02', 'mar': '03', 'apr': '04', 'may': '05',
                'jun': '06', 'jul': '07', 'aug': '08', 'sep': '09', 'oct': '10', 'nov': '11',
                'dec': '12'};
                split = startDate.split('-');
                var newStartDate = [split[2], months[split[1]], split[0]].join(',');

                split = endDate.split('-');
                var newEndDate = [split[2], months[split[1]], split[0]].join(',');

                var myDate=new Date();
                myDate.setFullYear(startDate);
                console.log(newStartDate);

                var myDateEnd=new Date();
                myDateEnd.setFullYear(endDate);

             if (myDate < myDateEnd) {
                alert ("Error !");
             }
        }


<form method="POST" id="myForm" onsubmit="compareDates()">
        <input id="startDate" />
        <input id="endDate" />
        <input type="Submit" />

    </form>
  • 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-09T13:23:27+00:00Added an answer on June 9, 2026 at 1:23 pm

    Just pass the string to a Date constructor. Much easier.

    var startDate = new Date(document.getElementById("startDate").value);
    var endDate = new Date(document.getElementById("endDate").value);
    

    Now you’ve got two full-fledged Date instances that you can work with as you please.

    function compare()
    {
        var startDate = new Date(document.getElementById("startDate").value);
        var endDate = new Date(document.getElementById("endDate").value);
    
        if (startDate.getTime() > endDate.getTime())
        {
            alert ("Error !");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well I know that this question may be asked many times but certainly I've
Well, first I know that this question was already asked several times. But I
I know, this question was asked many times and answered as well.. But, my
I know this kind of question has been asked a few times, but alot
So I know this question have been asked a lot , but I don't
I know this question is asked many times before but still can't find the
I know i have asked this question many times but i still havnt found
I know this question has been asked lots of times, but I am not
I know this question has been asked many and many times, but I have
I know, this question was asked hundred times, but none of the given solutions

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.