I have to calculate the duration of two dates with days and hours remaining.
If the two dates have different days, I need to return the duration in days and hours.
For example, given the following input:
2016-12-11T09:30:00.000Z and 2016-12-12T11:30:00.000Z
I would like to have this output:
1 day 2 hrs
How to achieve this using moment.js?
You can use moment-duration-format plug-in.
Just create moment object from your strings/dates, then get the difference in milliseconds using diff method in order to create a duration object. Use format method from moment-duration-format to print duration according your needs.
Here a working example:
// Create moment objects
var m1 = moment('2016-12-11T09:30:00.000Z');
var m2 = moment('2016-12-12T11:30:00.000Z');
// Get the difference in milliseconds
var diff = Math.abs( m1.diff(m2) );
// Format duration according your needs
console.log(moment.duration(diff).format("d [day] h [hrs]"));
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>
Related
how would you calculate the number of seconds since epoch in eiffel for a DATE_TIME object?
Assuming variable t is of type DATE_TIME (in UTC), the code to get the corresponding number of seconds since the epoch is
t.definite_duration (create {DATE_TIME}.make_from_epoch (0)).seconds_count
Note. If the calculation is performed rather often, it might be more efficient to create an object epoch of type DATE_TIME with create epoch.make_from_epoch (0) in advance and to use it in the calculation instead:
t.definite_duration (epoch).seconds_count
This seems to work:
make
-- Execute the example
local
l_date:DATE_TIME
l_seconds:INTEGER_64
do
create l_date.make_now
l_seconds := l_date.relative_duration (create {DATE_TIME}.make_from_epoch (0)).seconds_count
print("The number of seconds: " + l_seconds.out + "%N")
end
I am new in this library. I have 2 dates, and I would like to obtain the difference in days, minutes and hours between both. but for some reason the output is an erroneous data. I have based on this page
to get the exact difference and it is different to my result. what am I doing wrong?
var now=2018-07-04 00:37:02;
var then=2018-07-05 08:00:00;
moment.utc(moment(now,"YYYY/MM/DD HH:mm:ss").diff(moment(then,"YYYY/MM/DD
HH:mm:ss"))).format("HH:mm:ss");
output is : 16:37
There are two significant problems here:
a.diff(b) returns a - b, so it will be positive if a is later than b. You're using now.diff(then) effectively, so that will currently give a negative difference, and I suspect you're expecting it to be positive.
You're turning a duration in time into a date/time using moment.utc. The value just isn't a date/time, so shouldn't be treated as one. Instead, use moment.duration(...) to convert to a momentjs Duration object, which you can then handle however you want.
Here's a complete example, having broken out each of the conversions involved for readability:
var now = "2018-07-04 00:37:02";
var then = "2018-07-05 08:00:00";
var parsedNow = moment(now, "YYYY/MM/DD HH:mm:ss");
var parsedThen = moment(then, "YYYY/MM/DD HH:mm:ss");
var diff = parsedThen.diff(parsedNow);
var durationDiff = moment.duration(diff);
alert(durationDiff.toISOString());
Output: PT31H22M58S
That 31 hours is 1 day + 7 hours etc. While a duration has the concept of days, months, years etc, I don't see any way of normalizing a duration created from diff to "1 day 7 hours". (It could certainly be done, but only as far as days - you couldn't normalize to include months without providing more context.)
The service returns a past date-time in ISO 8601 format like 2018-03-23T08:00:00Z and a duration in PnYnMnDTnHnMnS format like: PT3H. From those 2 values I need to calculate the date-time of the end of the duration. So in my case it would be 2018-03-23T11:00:00Z.
I'm using moment.js for this. The problem is, when I'm tring to get the duration end date-time it returns a human readable string like in xy hours.
The problems I'm facing:
It returns wrong duration. If I have PT3H it should return in 3 hours but it returns in 9 hours instead.
The end date-time should be in milliseconds, currently its in "human readable form"
My code:
let dur = moment.duration("PT3H");
let myDate = moment("2018-03-23T08:00:00Z");
myDate.from(dur); // It returns "in 9 hours"
JsBin
You can add your duration to your date using add(Duration) and then use from.
Note that from accepts: Moment|String|Number|Date|Array while, in your code, you are passing a duration (dur).
You can use diff to get the difference in milliseconds.
If you want to get Unix timestamp of a moment object you can use valueOf().
Here a live sample:
let dur = moment.duration("PT3H");
let myDate = moment("2018-03-23T08:00:00Z");
let m2 = myDate.clone().add(dur);
console.log(m2.from(myDate));
let diff = m2.diff(myDate);
console.log(diff);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
Hi im currently using momentjs for my dates in my project and im having a bit of trouble when subtracting 2 dates.
Here are my sample dates:
2016-10-08 10:29:23
2016-10-08 11:06:55
ive tried using the diff and subtract from the guide docs of momentjs but i got nothing.
And what if the date subtracted dates are more than 24 hours?
Thnks in advance.
You are correct, you can use moment's diff function to subtract two dates (see my example on Plunker):
var date1 = moment('2016-10-08 10:29:23');
var date2 = moment('2016-10-08 11:06:55');
var diff = date2.diff(date1);
Diff will be equal to 2252000, the number of milliseconds between the two date. See documentation for more details.
You can pass a second argument to diff with the measurement to use (years, months, weeks, days, hours, minutes, and seconds), so if you want to know the number of minutes between the two dates you can write:
var diffInMinutes = date2.diff(date1, 'minutes');
And you get 37 minutes.
Background: I want to use coldfusion to find the total time a process takes by taking two timestamps and then adding all of the total times to create an average.
Question: What is the best way to take two timestamps and find out the difference in time by minutes.
Example:
Time Stamp #1: 2015-05-08 15:44:00.000
Time Stamp #2: 2015-05-11 08:52:00.000
So the time between the above timestamps would be:
2 Days 6 hours 52 mins = 3,292 minutes
I want to run this conversion on a handful of timestamp's and take the total minutes and divide to get an average.
To add more information to my question. 1. Yes the values are coming from a DB MSSQL. 2. I am actually going to be using the individual time differences and showing and overall average. So in my for loop each line will have a value like 3,292 (converted to mins or hours or days) and at the end of the for loop I want to show an average of all the lines shown on the page. Let me know if I need to add any other information.
Assuming your query is sorted properly, something like this should work.
totalMinutes = 0;
for (i = 2; i <= yourQuery.recordcount; i++)
totalMinutes +=
DateDiff('n'
, yourQuery.timestampField[i-1]
,yourQuery.timestampField[i]);
avgMinutes = totalMinutes / (yourQuery.recordcount -1);
Use the dateDiff() function
diffInMinutes = dateDiff('n', date1, date2);