Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I format time durations exactly using Moment.js?

I would like to do the following, given two dates in UTC formatting:

var start = "2014-01-13T06:00:00.0000000Z";
var end = "2014-01-13T14:16:04.0000000Z";

I would like to get the exact time span that passes between these two times, such as

8h 16m

I have tried using the following:

var duration = moment(moment(end) - moment(start)).format('hh[h] mm[m]');

But this does not work with days. Moreover, it does not work with days, since they are always >=1 even if <24 hours pass.

I have also tried twix.js to get the length, but its formatting doesn't support creating the format specified above, or I could not find the way to do so in its documentation. Basically I am looking for an exact version of twix.humanizeLength().

Moment.js's a.diff(b) provides only total durations, it can give me the length of the time span in minutes, hours or days, but not calculated using remainders.

My current solution is to use diff to create the ranges and then use modulo to calculate remainders, but this is not very elegant:

var days = moment(end).diff(start, 'days');
var hours = moment(end).diff(start, 'hours') % 24;
var minutes = moment(end).diff(start, 'minutes') % 60;

var duration = ((days > 0) ? days + 'd ' : '') + ((hours > 0) ? hours + 'h ' : '') + ((minutes > 0) ? minutes + 'm ' : ''); 

The question: Is there any smarter way to do this in either moment.js or twix.js, or should I take my time and develop my own moment.js plugin?

like image 510
ane Avatar asked Feb 10 '14 10:02

ane


People also ask

How do I change a moment date to a specific format?

Date Formatting Date format conversion with Moment is simple, as shown in the following example. moment(). format('YYYY-MM-DD'); Calling moment() gives us the current date and time, while format() converts it to the specified format.

What does format () do in moment?

The format() function takes in a string and replaces all instances of tokens with the corresponding date value. A token is a substring like 'YYYY' or 'd' that Moment knows to replace with a part of the date, like the year or the day of the month.

What is Moment () hour ()?

The moment(). hour() Method is used to get the hours from the current time or to set the hours. Syntax: moment().hour(); or. moment().


1 Answers

You can try using Durations, but I'm not sure if those have the capabilities you are looking for http://momentjs.com/docs/#/durations/

Also, you can always user moment's diff to get the difference in milliseconds and then format it to your needs. It is basically the same that you are doing, but you only call diff once.

function convertMilliSecondsIntoLegibleString(milliSecondsIn) {

    var secsIn = milliSecondsIn / 1000;
    var milliSecs = milliSecondsIn % 1000;

    var hours = secsIn / 3600,
    remainder = secsIn % 3600,
    minutes = remainder / 60,
    seconds = remainder % 60;


    return ( hours + "h: "
            +  minutes + "m: "
            + seconds +"s: " + milliSecs + "ms");
}
like image 176
jorge.alonso Avatar answered Oct 21 '22 13:10

jorge.alonso