-3

i want to check if today date is greater than specified date say 26th march 2017 note only one date is specified not both i researched on stack overflow the questions answer check between two specified date . i want to check

if(current date > specified date) {
// do this 
}

i tried converting into day but it was not flexible . I dint understand well

Rishabh Ahuja
  • 13
  • 1
  • 1
  • 3
  • 1
    Possible duplicated http://stackoverflow.com/q/492994/6568620 – Mohamed Abbas Mar 20 '17 at 06:07
  • i wrote in question other answers compare two specified dates not current date with other specifed date think before u answer – Rishabh Ahuja Mar 20 '17 at 06:10
  • @RishabhAhuja I guess you should read answer more carefully new Date(); will give you current date The link by Mohamed Abbas is sufficient to close your answer – Vinod Louis Mar 20 '17 at 06:14
  • 5
    Possible duplicate of [Compare two dates with JavaScript](http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – Vinod Louis Mar 20 '17 at 06:15

3 Answers3

5

One of the solution i would suggest is moment.js library.

It provides Query functions like isBefore(), isSame() and isAfter().

var today = moment('2010-10-20'),
     specifiedDate = moment('2010-10-21');
if(today.isBefore(specifiedDate)){
  console.log("Past Date");
}else{
  console.log("Future Date");
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.0/moment.min.js"></script>
ksyCho
  • 89
  • 4
2

var specific_date = new Date('2017-03-26');
var current_date = new Date();
if(current_date.getTime() > specific_date.getTime())
{
    console.log('current_date date is grater than specific_date')
}
else
{
    console.log('current_date date is lower than specific_date')
}
Kapila Perera
  • 837
  • 1
  • 11
  • 24
  • It's not precise. What happens if they are equals? You have to write "Current_date date is lower than or equal to the specific date". Or you can add else if (current_date.getTime() == specific_date.getTime()) – Avi Jul 22 '21 at 09:38
1

Use Moment.js to handle dates,

ex:

moment().isAfter('2014-03-26T01:14:00Z') // true
moment().isAfter('2017-03-26T01:14:00Z') // false
Dushyantha
  • 217
  • 2
  • 15