1

How to calculate number of days and hours difference between 2 days in AngularJS

dueOnDate: "2017-05-30T08:30:10.123+02:00"

currentDate: "2016-05-30T09:30:10.123+02:00"

Is there any inbuilt Angular function to achieve this?

Thanks in advance

Pratap A.K
  • 4,337
  • 11
  • 42
  • 79
  • Why do these types of questions come in batches? Perhaps [*Return Date in format ( x years, x months, x days)*](http://stackoverflow.com/questions/35189490/return-date-in-format-x-years-x-months-x-days-javascript) will help. Note that "2016-05-30T09:30:10.123+02:00" will not be correctly parsed by at least some implementations, you'll need to do it manually (or use a library). – RobG Feb 04 '16 at 06:44
  • 1
    Have a look at: http://momentjs.com/ – ashfaq.p Feb 04 '16 at 06:54
  • Thanks for the input...Let me try this once. – Pratap A.K Feb 04 '16 at 08:55

1 Answers1

2

As Robg said, you are currently dealing with a string which makes it more difficult to do calculations with. First thing I would do is start using the momentjs library which is very good for working with dates.

Then I would have a look at this question about hour difference, or this question and the answers because I think they have everything in them you need.

An example is:

var then = "04/09/2013 15:00:00";
var now = "02/09/2013 14:20:30";

var duration = moment.duration(then.diff(now));
var hours = duration.asHours();
Community
  • 1
  • 1
Daan van Hulst
  • 1,426
  • 15
  • 32