-1

I go through moment.js docs but not able to find if a date is previous date,

ex 1

var date='2018/09/28';
//now I want to compare this with today's date
var today =  moment(new Date()).format('YYYY/MM/DD');

this should return true

ex 2

var date='2018/10/02';
//now I want to compare this with today's date
var today =  moment(new Date()).format('YYYY/MM/DD');

this should return false;

I acheived this with javascript,

// I splitted days, months and year and compare with each other

but I want to know if there is something in moment to reduce my line of code?

  • a simple google should solve the problem right? – mckuok Oct 01 '18 at 07:57
  • 1
    possible duplicate https://stackoverflow.com/questions/21284312/moment-js-check-if-a-date-is-today-or-in-the-future – Sukhmeet Singh Oct 01 '18 at 07:59
  • 1
    I think your answer is already present [here](https://stackoverflow.com/questions/30668373/moment-js-test-if-a-date-is-today-yesterday-within-a-week-or-two-weeks-ago) – Abdul Basit Oct 01 '18 at 07:59
  • 1
    You have gone through momentjs docs but couldn't find it?? I suggest you check the docs once again plz. – Vivek Athalye Oct 01 '18 at 07:59
  • @mckuok a simple google did not solved my problem thats why i asked it on stack –  Oct 01 '18 at 08:12

1 Answers1

12

You can use isBefore to compare two dates in moment.

console.log(moment('2018/09/28', "YYYY/MM/DD").isBefore(moment()));
console.log(moment('2018/10/02', "YYYY/MM/DD").isBefore(moment()));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51