nikitha nikitha - 1 year ago 186
Javascript Question

Compare two dates angularjs

I have two dates to be compared in the following format
the response coming from backend service has following date format

alignFillDate - 2015-06-09
pickUpDate - 2015-05-10

so, front end needs to check the condition is if pickUpDate is less then the alignFillDate, we will increase the alignFillDate by 30 days, i.e, we increment the pickUpDate to next month(30 days from now ) and show different text on view

How, do i achieve this using angular and javascript. ? how does my html and controller needs to changed for this date calculation

Answer Source

You save those date strings as Date objects, do a comparison with vanilla javascript and assign to scope or this.

   var alignFillDate = new Date("2015-06-09");
  var pickUpDate = new Date("2015-05-10");

  if (pickUpDate < alignFillDate) {
    alignFillDate = alignFillDate.setDate(alignFillDate.getDate() + 30);

  $scope.pickUpDate = pickUpDate;
  $scope.alignFillDate = alignFillDate;

Here is a plunk that does what you are trying to do

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download