Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check whether the date entered by the user is current date or the future date

Tags:

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;     } }    
like image 298
Ryan decosta Avatar asked Sep 10 '13 07:09

Ryan decosta


People also ask

How do you validate the selected date is a current date or not?

Approach 1: Get the input date from user (var inpDate) and the today's date by new Date(). Now, use . setHours() method on both dates by passing parameters of all zeroes. All zeroes are passed to make all hour, min, sec and millisec to 0.

How can I compare current date and future date in Java?

Java 8 onward date API provides isBefore(), isEqual(), isAfter() and these methods take an input date value which is then compared to another date value to check whether the input date is before, equal or after another date value. That's all about checking the given date is past, future or today's date.


1 Answers

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.

like image 52
Reinherd Avatar answered Sep 24 '22 19:09

Reinherd