4

I get 2 dates from MySQL something like this:

Start: 2015-11-01 22:56:59 End: 2015-11-03 00:00:00

Also I get dates from object array; here is what one object looks like:

Array[5]
0: Object
$$hashKey: "object:9"
created_at: "2015-10-23 03:36:11"
expiration_date: "2015-11-03 00:00:00"
id: 1
name: "TEST PROJECT"

I need to find how many days left with AngularJS...

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Vladimir Djukic
  • 2,042
  • 7
  • 29
  • 60

4 Answers4

7

Its simple. Use the function as:

$scope.calcDiff = function(firstDate, secondDate){
    var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds    
    var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
}
Emir Marques
  • 2,603
  • 2
  • 16
  • 22
7

Consider using moment.js for all your javascript date and time manipulations.

With the moment library it is as easy as:

moment(date1).diff(moment(date2), 'days')
3

You just need to write a js function, that do all calculation instead of you.

<label>{{diffDate(date1, date2) }} </label>


  $scope.diffDate = function(date1, date2){
          var dateOut1 = new Date(date1); // it will work if date1 is in ISO format
           var dateOut2 = new Date(date2);

          var timeDiff = Math.abs(date2.getTime() - date1.getTime());
          var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
          alert(diffDays);
          return dateOut;
    };

If your data is not in ISO format, you have to set it explicitly.

var dateString = "2015-01-16 22:15:00";
var date = Date.parse(dateString, "yyyy-MM-dd HH:mm:ss");
vvg
  • 6,325
  • 19
  • 36
0

You can use amDifference filter. Don't forget to inject $filter

let dif = $filter('amDifference')(date1, date2, 'days');