user1048676
user1048676

Reputation: 10066

Check if date is in the past Javascript

All, I'm using the jQuery UI for the date picker. I'm trying to check with javascript though that the date the user has entered is in the past. Here is my form code:

<input type="text" id="datepicker" name="event_date" class="datepicker">

Then how would I check this with Javascript to make sure it isn't a date in the past? Thanks

Upvotes: 79

Views: 164795

Answers (6)

Matt Walterspieler
Matt Walterspieler

Reputation: 2171

You can use isPast(date) method from date-fns library.

import { isPast } from 'date-fns'
console.log(new Date('1991-06-17'));
// returns true.
console.log(new Date('2191-06-17'));
// returns false.

More info about the method: https://date-fns.org/v2.29.3/docs/isPast

Upvotes: -2

Muhammad Abdullah
Muhammad Abdullah

Reputation: 4485

Simply convert the dates into milliseconds and subtract

let givenDate1 =   new Date("10/21/2001")  // Past Date
let givenDate2 =   new Date("10/21/2050") // future Date

If diff is positive, then given date is PAST

 let diff = new Date().getTime() - givenDate1.getTime();
  if (diff > 0) {
     console.log('Given Date givenDate1 is in Past');
   }

If diff is negative, then given date is Future

 let diff = new Date().getTime() - givenDate2.getTime();
  if (diff < 0) {
     console.log('Given Date givenDate2 is in Future');
   }

Upvotes: 6

Mike
Mike

Reputation: 1673

To make the answer more re-usable for things other than just the datepicker change function you can create a prototype to handle this for you.

// safety check to see if the prototype name is already defined
Function.prototype.method = function (name, func) {
    if (!this.prototype[name]) {
        this.prototype[name] = func;
        return this;
    }
};
Date.method('inPast', function () {
    return this < new Date($.now());// the $.now() requires jQuery
});

// including this prototype as using in example
Date.method('addDays', function (days) {
    var date = new Date(this);
    date.setDate(date.getDate() + (days));    
    return date;
});

If you dont like the safety check you can use the conventional way to define prototypes:

Date.prototype.inPast = function(){
    return this < new Date($.now());// the $.now() requires jQuery
}

Example Usage

var dt = new Date($.now());
var yesterday = dt.addDays(-1);
var tomorrow = dt.addDays(1);
console.log('Yesterday: ' + yesterday.inPast());
console.log('Tomorrow: ' + tomorrow.inPast());

Upvotes: 2

Amadan
Amadan

Reputation: 198304

$('#datepicker').datepicker().change(evt => {
  var selectedDate = $('#datepicker').datepicker('getDate');
  var now = new Date();
  now.setHours(0,0,0,0);
  if (selectedDate < now) {
    console.log("Selected date is in the past");
  } else {
    console.log("Selected date is NOT in the past");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<input type="text" id="datepicker" name="event_date" class="datepicker">

Upvotes: 144

Srinivasan S
Srinivasan S

Reputation: 179

var datep = $('#datepicker').val();

if(Date.parse(datep)-Date.parse(new Date())<0)
{
   // do something
}

Upvotes: 17

Vignesh
Vignesh

Reputation: 1518

function isPrevDate() {
    alert("startDate is " + Startdate);
    if(Startdate.length != 0 && Startdate !='') {
        var start_date = Startdate.split('-');
        alert("Input date: "+ start_date);
        start_date=start_date[1]+"/"+start_date[2]+"/"+start_date[0];
        alert("start date arrray format " + start_date);
        var a = new Date(start_date);
        //alert("The date is a" +a);
        var today = new Date();
        var day = today.getDate();
        var mon = today.getMonth()+1;
        var year = today.getFullYear();
        today = (mon+"/"+day+"/"+year);
        //alert(today);
        var today = new Date(today);
        alert("Today: "+today.getTime());
        alert("a : "+a.getTime());
        if(today.getTime() > a.getTime() )
        {
            alert("Please select Start date in range");
            return false;
        } else {
            return true;
        }
    }
}

Upvotes: -11

Related Questions