Check Given Date Greater than Current Date JavaScript/JQuery

When you are building any form where user have to enter some date, then you may have requirement to allow only future dates / past dates.

So here i am going to explain how to check whether a user has entered date which is greater than today's date or less than today's date.

Conside you have a TextBox to allow user to enter a date and a Button to submit the data.

Enter Date(in mm-dd-yyyy format) : <input type="text" id="txtdate" />

        <input value="SUBMIT" id="btnsubmit" onclick="checkDate();"/>

The javascript/ JQuery code to validate the date is

 function checkDate() {
            var EnteredDate = document.getElementById("txtdate").value; //for javascript

            var EnteredDate = $("#txtdate").val(); // For JQuery

            var date = EnteredDate.substring(0, 2);
            var month = EnteredDate.substring(3, 5);
            var year = EnteredDate.substring(6, 10);

            var myDate = new Date(year, month - 1, date);

            var today = new Date();

            if (myDate > today) {
                alert("Entered date is greater than today's date ");
            }
            else {
                alert("Entered date is less than today's date ");
            }
        }

In this way you can check wether the entered date is Greater/Less than the current date.

Hope this post helps you.

For more posts regarding JavaScript/JQuery See this: Javascript/JQuery

7 comments:

  1. which jquery plugin we should use here , Could you please post the jquery pug-in link

    ReplyDelete
  2. Hi sidhu.

    Include the following script in your head tag. This includes jQuery in your project.

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    ReplyDelete
  3. Hello Ranadheer,
    This code is not working on my website. I just want a validation for current date and future date, if past date is inserted it should be validate and throw alert.
    Can you plz help me on this.

    Siddharth Thorat

    ReplyDelete
    Replies
    1. Hi Siddarth,
      First you need to convert to dates to same format. you can convert entered date like this.
      var enteredDate = new date("entered date");

      then compare with today's date enteredDate < new Date()

      Thanks,

      Hope it may help you. please revert back if you have any queries.

      Delete
  4. Hi Everyone,

    Here is the code to validate the future date against the current date.
    In my code sample, i am restricting the future date entry.

    function CheckForFutureDate(chkdate) {
    debugger;
    var edate = chkdate.split("/");
    var spdate = new Date();
    var sdd = spdate.getDate();
    var smm = spdate.getMonth() + 1;

    var syyyy = spdate.getFullYear();
    var today1 = new Date(syyyy, smm, sdd);
    var today = new Date(syyyy, smm, sdd).getTime();
    if (edate[0] < 10)
    edate[0] = parseInt(edate[0]);
    if (edate[1] < 10)
    edate[1] = parseInt(edate[1]);
    var e_date1 = new Date(edate[2], edate[0], edate[1]);
    var e_date = new Date(edate[2], edate[0], edate[1]).getTime();


    if (e_date > today) {
    alert("Input date cannot be greater than today's date");
    return false;
    }
    else {
    return true;
    }
    }

    Chkdate is the date value from the textbox.
    You can call this function on onkeypress event or onclick based on your requirement.

    ReplyDelete
  5. pls help me with this-

    Apply a validation on the date field so that if a user selects the date less than the current date,
    an alert message is displayed.

    ReplyDelete