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...

3
  • you could use momentjs to get the difference between the dates and then display them in a useful format using a filter Commented Nov 1, 2015 at 21:37
  • possible duplicate of stackoverflow.com/a/3224854/2435473 Commented Nov 1, 2015 at 21:40
  • Not duplicat I need to find diffrence between dates from MySQL and to get diffirence directly inside template. Commented Nov 1, 2015 at 21:42

4 Answers 4

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)));
}
Sign up to request clarification or add additional context in comments.

2 Comments

Not simple because I get date from mysql in this format: 2015-11-01 22:56:59 and my dates are inside object array...
Call as $scope.calcDiff(new Date("2015-10-23 03:36:11"), new Date("2015-11-03 00:00:00"));
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')

2 Comments

Problem with It is my dates are inside array of objects, so I can only find diffrence directly inside template or to set new value inside each object...
You can use the above in template, just add curlys: {{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");

3 Comments

I get date from mysql in this format: 2015-11-01 22:56:59 and my dates are inside object array...
You can put into the function an array as parameter and work with that.
But how I will set up new value for each object inside array? I updated main post how object look like
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.