7

I have this code that I want to check if a date is in the past. I want to check it as soon as the date is entered, before form submission.

<input id="datepicker" onchange="checkDate()" required class="datepicker-input" type="text" data-date-format="yyyy-mm-dd" >

<script type="text/javascript">
 function checkDate() {
   var selectedDate = document.getElementById('datepicker').value;
   var now = new Date();
   if (selectedDate < now) {
    alert("Date must be in the future");
   }
 }
</script>

This does not work, if I enter a date in the past (e.g. 2014-12-03) it does not display the alert.

Alex
  • 89
  • 1
  • 1
  • 4

7 Answers7

20

All you need to do is convert the string produced by the <input> into a Date using the Date constructor new Date("2014-06-12")

 function checkDate() {
   var selectedText = document.getElementById('datepicker').value;
   var selectedDate = new Date(selectedText);
   var now = new Date();
   if (selectedDate < now) {
    alert("Date must be in the future");
   }
 }
<input id="datepicker" onchange="checkDate()" required class="datepicker-input" type="date" data-date-format="yyyy-mm-dd" >
Migwell
  • 18,631
  • 21
  • 91
  • 160
  • It's not working. But it seems the issue is something else. Even if I do this: $("#datepicker").change(function() { alert( "test" ); }); the alert doesn't show – Alex Dec 31 '14 at 00:35
  • It's working for me? Try typing 2014-06-12 into the box and pressing enter (you need to press enter or change focus of the control to trigger the onchange event) – Migwell Dec 31 '14 at 00:38
  • ah ok. I removed the date picker plugin and it worked. How can I make it work using the date picker plugin – Alex Dec 31 '14 at 00:41
  • 2
    If you want to use a date picker plugin, use jQuery UI's one, and then you can set minDate and maxDate instead of your own validation function. See [this example](http://jqueryui.com/datepicker/#min-max) – Migwell Dec 31 '14 at 00:44
  • add now.setHours(0,0,0,0); so that you will not get alert for today date – Hari Prasanth Mar 03 '21 at 11:48
1

The date() function returns a string. Try converting both dates to integers first using the Date.parse() function:

<input id="datepicker" onchange="checkDate()" required class="datepicker-input" type="text" data-date-format="yyyy-mm-dd" >

<script type="text/javascript">
   function checkDate() {
       var selectedDate = document.getElementById('datepicker').value;
       var now = new Date();
       var dt1 = Date.parse(now),
       dt2 = Date.parse(selectedDate);
       if (dt2 < dt1) {
            alert("Date must be in the future");
       }
 }
</script>
Cedric Ipkiss
  • 5,662
  • 2
  • 43
  • 72
  • It seems the issue is something else, because even if I do this: $("#datepicker").change(function() { alert( "test" ); }); the alert doesn't show – Alex Dec 31 '14 at 00:37
  • I'll suggest you look at your browser's console; you might have an error elsewhere – Cedric Ipkiss Dec 31 '14 at 00:38
1

For those who use type="date" (introduced with HTML5) instead of type="text", it can be done this way:

 function checkDate() {
   var date = this.value;
   if(date.valueAsDate <= new Date()) {
       //Date in the past
   } else {
       //Date in the future
   }
}
t.m.
  • 1,430
  • 16
  • 29
0

Your code is really close, try this instead:

var selectedDate = $('#datepicker').datepicker('getDate');
var now = new Date();
if (selectedDate < now) {
  // selected date is in the past
}
Alex J
  • 1,029
  • 6
  • 8
  • 1
    Please try to compare date by using getTime function... if (selectedDate.getTime() < now.getTime()) { ... } – joydesigner Dec 31 '14 at 00:26
  • 1
    As far as I know, that solution depends on having jQuery UI installed. It's basically converting your input field to a datepicker widget. It will work, but it's totally unnecessary. – Migwell Dec 31 '14 at 00:28
0

Simple mine function working ...

function check_not_past_date_time(enter_date) {
    var a =new Date(enter_date);
    var now =new Date();
    if(a>now){
        return true;
    }else {
        return false;
    }
}
Mostafa Mahmoud
  • 153
  • 3
  • 7
0

You can use


_isDate1LowerThanDate2(date1, date2) {
    return new Date(date1) < new Date(date2);
  }

Orsos Patrick
  • 389
  • 3
  • 5
0

Before comparing dates, they should be set to 0 hours. This would ensure difference between them is in multiples of 1 day.

const isPastDate = (date) => {
    const specificDate = new Date(date).setHours(0, 0, 0, 0);
    const today = new Date().setHours(0, 0, 0, 0);
    return specificDate < today;
}

console.log(isPastDate("2021-08-10T23:59:00"));
ritz
  • 4,747
  • 2
  • 15
  • 13