check whether the date entered by the user is current date or the future date
Asked Answered
S

7

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;
    }
}   
Schramke answered 10/9, 2013 at 7:25 Comment(1)
+target > +now. Coerce the Date to a number (in milliseconds).Foehn
K
62

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.

Kironde answered 10/9, 2013 at 7:28 Comment(5)
but it doesn't check for the current dateSchramke
uhm, what? when you do a new Date() its taking the system's actual date & time.Kironde
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 invalidSchramke
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 validationBooher
I don't like anything about this answer. Date are way more complex than this.Pillory
J
16

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)
};
Janejanean answered 15/3, 2019 at 18:31 Comment(0)
C
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
Cari answered 25/2, 2016 at 7:49 Comment(3)
Ok but why would you add this when there's a better solution already given?Cymar
Implemented same solution. Working fine..ThanksBlintze
@ShashikanthHR plz voteCari
F
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
}
Forfeiture answered 7/3, 2019 at 22:37 Comment(0)
F
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
    }

}
Fortify answered 23/8, 2019 at 8:1 Comment(0)
D
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");  
  }
}
Driest answered 14/6, 2021 at 8:36 Comment(0)
T
-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

Tresa answered 9/6, 2021 at 21:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.