0

I want to compare a date (mydate) with today. I tried

new Date(mydate) < new Date();

which works in all cases apart from the case my date equals today. In that case the above returns true since it compares the time included. However, I want to compare only the date, not the time.

I tried moment.js as well:

moment(mydate).format('DD-MM-YYYY')<moment().format('DD-MM-YYYY')

however, not even this one did work. What do I have to write?

Unknown developer
  • 5,414
  • 13
  • 52
  • 100
  • 2
    `YYYYMMDD` format should make it work. – sp00m Jan 17 '17 at 12:32
  • Possible duplicate of [Comparing date part only without comparing time in JavaScript](http://stackoverflow.com/questions/2698725/comparing-date-part-only-without-comparing-time-in-javascript) – sabithpocker Jan 17 '17 at 12:33
  • 1
    `moment(date2).isAfter(date1, 'day');` use day as granularity level, as copied from the answer over there. The docs are [here](http://momentjs.com/docs/#/query/is-before/) Use moment as they take cares of DST issues as well. – sabithpocker Jan 17 '17 at 12:35
  • sabithpocker is right. This is the safest and simplest way. It uses the internal representation of the date to compare, rather than any particular format (the format is just for display, not for calculation). Comparing the formatted strings as you're doing is never going to work, because it does a simple string comparison, not a date comparison. In fact in that scenario JS doesn't even know it's a date, it's just two arbitrary strings. – ADyson Jan 17 '17 at 12:37

2 Answers2

2

You can use moment function isSame and provide the second argument as the granularity level you want for your comparison. The granularity levels can be strings like, 'year', 'month', 'day'

moment('2010-01-01').isSame('2010-02-01', 'day'); 

You can also look into similar functions documented over there which might help you better with your requirement.

  1. Is Before
  2. Is Same
  3. Is After
  4. Is Same or Before
  5. Is Same or After
  6. Is Between

All of these supports different granularity levels.

As a sidenote, to create a date in your supplied format do:

moment("25-12-1995", "DD-MM-YYYY");
sabithpocker
  • 15,274
  • 1
  • 42
  • 75
0

You can simply do following using moment js

$(document).ready(function() {
  
  $('.compare').click(function(e) {
  
    var date = $('#date').val();
  
    var now = moment().format('YYYY-MM-DD');
    var then = moment(date).format('YYYY-MM-DD');

    if (now > then) {
      $('.result').text('Date is past');
    } else if(now == then) {
      $('.result').text('Date is today');
    } else {
      $('.result').text('Date is future');
    }

  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>
<input type="text" name="date" id="date" value="2014-12-18"  placeholder="yyyy-mm-dd">
<button class="compare">Compare date to current date</button>
<br>
<div class="result"></div>
Ashish Panchal
  • 440
  • 3
  • 11