27

I was browsing through the net to find a javascript function which can check whether the date entered by the user is current date or the future date but i didn't found a suitable answer so i made it myself.Wondering If this can be achieved by one line code.

 function isfutureDate(value) 
    {    
    var now = new Date;
    var target = new Date(value);

    if (target.getFullYear() > now.getFullYear()) 
    {
        return true;
    }
    else if(target.getFullYear() == now.getFullYear()) 
    {
    if (target.getMonth() > now.getMonth()) {
    return true;
    } 
    else if(target.getMonth() == now.getMonth())
    {
    if (target.getDate() >= now.getDate()) {
        return true;
    }
    else
    {
        return false
    }
    }


    }

   else{
    return false;
    }
}   
Ryan decosta
  • 455
  • 1
  • 4
  • 10

7 Answers7

61

You can compare two dates as if they were Integers:

var now = new Date();
if (before < now) {
  // selected date is in the past
}

Just both of them must be Date.

First search in google leads to this: Check if date is in the past Javascript

However, if you love programming, here's a tip:

  1. A date formatted like YYYY-MM-DD could be something like 28-12-2013.
  2. And if we reverse the date, it is 2013-12-28.
  3. We remove the colons, and we get 20131228.
  4. We set an other date: 2013-11-27 which finally is 20131127.
  5. We can perform a simple operation: 20131228 - 20131127

Enjoy.

Community
  • 1
  • 1
Reinherd
  • 5,476
  • 7
  • 51
  • 88
  • 1
    but it doesn't check for the current date – Ryan decosta Sep 10 '13 at 07:31
  • 3
    uhm, what? when you do a new Date() its taking the system's actual date & time. – Reinherd Sep 10 '13 at 07:32
  • 2
    the reason behind it is that new Date(); is returning date and time and user input is only date format which is converted into date time by var target = new Date(value); so comparing them for the current date becomes invalid – Ryan decosta Sep 10 '13 at 07:40
  • yep, the even more glaring issue is it's based on the user's system time set up. If they modify it at all that throws off the validation – iamaword Feb 16 '21 at 16:29
  • I don't like anything about this answer. Date are way more complex than this. – BentOnCoding Nov 08 '22 at 21:24
15

here's a version that only compares the date and excludes the time.

Typescript

const inFuture = (date: Date) => {
    return date.setHours(0,0,0,0) > new Date().setHours(0,0,0,0)
};

ES6

const inFuture = (date) => {
    return date.setHours(0,0,0,0) > new Date().setHours(0,0,0,0)
};
denov
  • 11,180
  • 2
  • 27
  • 43
3

try out this

function isFutureDate(idate){
var today = new Date().getTime(),
    idate = idate.split("/");

idate = new Date(idate[2], idate[1] - 1, idate[0]).getTime();
return (today - idate) < 0 ? true : false;
}

Demo

console.log(isFutureDate("02/03/2016")); // true
console.log(isFutureDate("01/01/2016")); // false
Khalid Habib
  • 1,100
  • 1
  • 16
  • 25
1

ES6 version with tolerable future option.

I made this little function that allows for some wiggle room (incase data coming in is from a slightly fast clock for example).

It takes a Date object and toleranceMillis which is the number of seconds into the future that is acceptable (defaults to 0).

const isDistantFuture = (date, toleranceMillis = 0) => {
    // number of milliseconds tolerance (i.e. 60000 == one minute)
    return date.getTime() > Date.now() + toleranceMillis
}
Seth
  • 708
  • 6
  • 8
1

try this

function IsFutureDate(dateVal) {
    var Currentdate = new Date();
        dateVal= dateVal.split("/");
    var year = Currentdate.getFullYear();
    if (year < dateVal[2]) {
        return false;//future date

    }        
    else {
        return true; //past date
    }

}
Prajakta Kale
  • 392
  • 3
  • 19
0

In my case, I used DD-MM-YYYY format dates to compare and it gives an error since the behaviour of "DD-MM-YYYY" is undefined. So I convert it to a compatible format and compare it. And also if you need to compare only the dates and not time, you need to set time parameters to zero.

var inputDateVal = "14-06-2021";

if (inputDateVal != null && inputDateVal != '') {
  var dateArr = inputDateVal.split("-");
  var inputDate = new Date('"' + dateArr[2] + "-" + dateArr[1] + "-" + dateArr[0] + '"').setHours(0, 0, 0, 0);
  
  var toDay = new Date().setHours(0, 0, 0, 0);
  
  if(inputDate > toDay){
    console.log("Date is a future date");
  }else if(inputDate== toDay){  
    console.log("Date is equal to today");
  }else{
    console.log("Date is a past date");  
  }
}
-1

You can use moment.js library

 let dateToBeCompared = "10/24/2021"; // "DD/MM/YYYY" format
 // For past dates
  moment(dateToBeCompared, "DD/MM/YYYY").isBefore(moment(new Date(), "DD/MM/YYYY"), 
'day')
 // For same dates
  moment(dateToBeCompared, "DD/MM/YYYY").isSame(moment(new Date(), "DD/MM/YYYY"), 
'day')
 // For future dates
  moment(dateToBeCompared, "DD/MM/YYYY").isAfter(moment(new Date(), "DD/MM/YYYY"), 
'day');

There are other functions like also like isSameOrAfter() and isSameOrBefore() Have a look at here

Aryesh
  • 386
  • 2
  • 16