Search code examples
javascriptangularjsmomentjsangular-moment

how to compare date using moment in angular js?


I am using moment js in angular to compare date .I am selecting first date from type="date" and second is hard corded string in date format. I want to compare both using moment lib on button click . I tried link this

https://plnkr.co/edit/RgpjNMFCA03A5quITpoz?p=preview

$scope.check = function(){
    //alert('==')
    var time1 = "20-06-2017";
    var time2 =moment($scope.dateV,"MM-DD-YYYY");
    console.log(time2)
  }

I want to check is dates are equal or not ?


Solution

  • First, you need to parse your date strings (according to the format you need them to be in) and then use moment's isSame function to check whether the two dates are same or not.

    var time1 = moment("20-06-2017", "DD-MM-YYYY");
    var time2 = moment($scope.dateV, "DD-MM-YYYY");
    console.log(moment(time1).isSame(time2))
    

    Hope this helps :)

    PS - Updated a fork of your Plunker code