Get the time difference between two datetimes

time calculator minutes
time between two times
datetime difference c#
c# time difference between two times
time difference calculator milliseconds
number of days between two dates excel
calculate the time difference between two times in hours and minutes in c#
date calculator between two dates

I know I can do anything and some more envolving Dates with momentjs. But embarrassingly, I'm having a hard time trying to do something that seems simple: geting the difference between 2 times.

Example:

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

//expected result:
"00:39:30"
```

what I tried:

```var now = moment("04/09/2013 15:00:00");
var then = moment("04/09/2013 14:20:30");

console.log(moment(moment.duration(now.diff(then))).format("hh:mm:ss"))
//outputs 10:39:30
```

I do not understand what is that "10" there. I live in Brazil, so we are utc-0300 if that is relevant.

The result of `moment.duration(now.diff(then))` is a duration with the correct internal values:

``` days: 0
hours: 0
milliseconds: 0
minutes: 39
months: 0
seconds: 30
years: 0
```

So, I guess my question is: how to convert a momentjs Duration to a time interval? I sure can use

```duration.get("hours") +":"+ duration.get("minutes") +:+ duration.get("seconds")
```

but i feel that there is something more elegant that I am completely missing.

update looking closer, in the above example `now` is:

```Tue Apr 09 2013 15:00:00 GMT-0300 (E. South America Standard Time)…}
```

and `moment(moment.duration(now.diff(then)))` is:

```Wed Dec 31 1969 22:39:30 GMT-0200 (E. South America Daylight Time)…}
```

I am not sure why the second value is in Daylight Time (-0200)... but I am sure that i do not like dates :(

update 2

well, the value is -0200 probably because 31/12/1969 was a date where the daylight time was being used... so thats that.

This approach will work ONLY when the total duration is less than 24 hours:

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

moment.utc(moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss")

// outputs: "00:39:30"
```

If you have 24 hours or more, the hours will reset to zero with the above approach, so it is not ideal.

If you want to get a valid response for durations of 24 hours or greater, then you'll have to do something like this instead:

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

var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");

// outputs: "48:39:30"
```

Note that I'm using the utc time as a shortcut. You could pull out `d.minutes()` and `d.seconds()` separately, but you would also have to zeropad them.

This is necessary because the ability to format a `duration` objection is not currently in moment.js. It has been requested here. However, there is a third-party plugin called moment-duration-format that is specifically for this purpose:

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

var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = d.format("hh:mm:ss");

// outputs: "48:39:30"
```

How do I get the time difference between two DateTime objects , The following example demonstrates how to do this: DateTime a = new DateTime​(2010, 05, 12, 13, 15, 00); DateTime b = new DateTime(2010,  DATEDIFF does not guarantee that the full number of the specified time units passed between 2 datetime values: -- Get difference in hours between 8:55 and 11:00 SELECT DATEDIFF (hh, '08:55', '11:00'); -- Returns 3 although only 2 hours and 5 minutes passed between times -- Get difference in months between Sep 30, 2011 and Nov 02, 2011 SELECT DATEDIFF (mm, '2011-09-30', '2011-11-02')-- Returns 2 although only 1 month and 2 days passed between dates

Your problem is in passing the result of moment.duration() back into moment() before formatting it; this results in moment() interpreting it as a time relative to the Unix epoch.

It doesn't give you exactly the format you're looking for, but

`moment.duration(now.diff(then)).humanize()`

would give you a useful format like "40 minutes". If you're really keen on that specific formatting, you'll have to build a new string yourself. A cheap way would be

`[diff.asHours(), diff.minutes(), diff.seconds()].join(':')`

where `var diff = moment.duration(now.diff(then))`. This doesn't give you the zero-padding on single digit values. For that, you might want to consider something like underscore.string - although it seems like a long way to go just for a few extra zeroes. :)

Calculate Date/Time Difference, Show the difference between two dates. Use this calculator to find the number of days, hours, minutes and seconds between 2 dates. From date/time: There are two approaches that you can take to present the results in the standard time format (hours : minutes : seconds). You use the subtraction operator (-) to find the difference between times, and then do either of the following: Apply a custom format code to the cell by doing the following: Select the cell.

```var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') //[days, years, months, seconds, ...]
//Result 1
```

Worked for me

How to calculate the time difference between two datetime objects in , Python's program to calculate time difference between two datetime objects. import datetime. from datetime import timedelta. datetimeFormat  The difference between these two properties is mainly seen when the time difference is more than 1 day. The Hours property will only report the actual hour difference between the two dates. So if two dates differed by 100 years but occurred at the same time in the day, hours would return 0.

If you want difference of two timestamp into total days,hours and minutes only, not in months and years .

```var now  = "01/08/2016 15:00:00";
var then = "04/02/2016 14:20:30";
var diff = moment.duration(moment(then).diff(moment(now)));
```

diff contains 2 months,23 days,23 hours and 20 minutes. But we need result only in days,hours and minutes so the simple solution is:

```var days = parseInt(diff.asDays()); //84

var hours = parseInt(diff.asHours()); //2039 hours, but it gives total hours in given miliseconds which is not expacted.

hours = hours - days*24;  // 23 hours

var minutes = parseInt(diff.asMinutes()); //122360 minutes,but it gives total minutes in given miliseconds which is not expacted.

minutes = minutes - (days*24*60 + hours*60); //20 minutes.
```

Final result will be : 84 days, 23 hours, 20 minutes.

Calculate the difference between two times - Excel, There are several ways to calculate the difference between two times. how to use these functions, see TEXT function and Display numbers as dates or times. Solved: I have two columns, both with dates and times in the same format. I want to create a new column to calculate the days/times differences

When you call `diff`, moment.js calculates the difference in milliseconds. If the milliseconds is passed to `duration`, it is used to calculate duration which is correct. However. when you pass the same milliseconds to the `moment()`, it calculates the date that is milliseconds from(after) epoch/unix time that is January 1, 1970 (midnight UTC/GMT). That is why you get 1969 as the year together with wrong hour.

```duration.get("hours") +":"+ duration.get("minutes") +":"+ duration.get("seconds")
```

So, I think this is how you should do it since moment.js does not offer `format` function for duration. Or you can write a simple wrapper to make it easier/prettier.

Calculate time in Excel: time difference, add, subtract and sum times, Easily find difference between two dates in Excel To get the time difference in a single time unit (hours ,minutes or seconds), you can perform  DATEDIFF calculates the whole difference between two dates. In other words, if you choose the interval to be minutes a difference is expressed in minutes even if the difference is greater than a single hour. This introduces complexity in the calculation since in each date part, the previous date part value needs to be taken into account.

JavaScript: Get time differences in hours between two dates , Write a JavaScript function to get time differences in hours between two dates. Test Data: dt1 = new Date("October 13, 2014 08:11:00"); dt2 = new  Alternatively, you can calculate time difference by simply subtracting the start time from the end time (e.g. =B2-A2), and then apply the following time format to the cell: d "days," h "hours," m "minutes and" s "seconds"

How Do I find the difference between two datetimes?, Using the Minute() function on both date times and subtracting the two. I noticed the datetime format for the fields in question are not in the same  As datetime variables are recorded in seconds, an alternative is to use the fact that there are 60 seconds in a minutes and just do the arithmetic directly (datetime2 - datetime1)/60 provides minutes with decimal fractions. FLOOR((datetime2 - datetime1)/60) provides the whole minute durations.

Excel formula: Get days, hours, and minutes between dates, To calculate and display the days, hours, and minutes between two dates, you can use the TEXT function with a little help from the INT function.

• "but I am sure that i do not like dates" :P Did you ever read this: nodatime.org/unstable/userguide/trivia.html ?
• @TML - I clearly said it wouldn't work for durations of 24 hours or longer.
• This worked fine. momentjs is such a elegant library it surprises me it does not have a more simple way but maybe what I am doing is not so "normal". I probably would not have more than 24 hours but it is nice to have the option if it happens. Thanks for your help.
• you'll have to use `asHours` instead of `hours`, and pull off the decimals. Otherwise it would have the same problem you caught in my answer. :)
• humanize() gives a nice touch but I ended up using Matt suggestion. Thanks for the tip.
• Thanks for pointing that out, @MattJohnson - I hadn't even realized .asHours and .hours were different, thought one was just a shorter name for the other, but on closer inspection, you're correct.
• Note, it would be wise to use: `[Math.floor(diff.asHours()), diff.minutes(), diff.seconds()].join(':')` in order to get the desired result. without a decimal in the first part
• this only works for dates. OP wanted to have the time difference
• This is the simplest solution which works. Yes, with timestamps too.
• I don't see moment usage anywhere in you answer.
• Better solution! :)