Sum a row with a time formatted like "HH:mm:ss"

Sum a row with a time formatted like "HH:mm:ss"

PhilouPhilou Posts: 24Questions: 4Answers: 0

Hi,

I try to get the SUM of a row filled with hours as duration formated as "HH:mm:ss" and get the result back as "HH:mm".
I cannot get the right sequence of that -> .pluck.moment.duration('Heures').asSeconds().
if you just try to sum a row with "HH:mm:ss" the result is wrong. I try to format in first instance the time in seconds to get the right SUM, and then format the result back as "HH:mm"
Is there an more easy way ? or is it the right one.

var Hours = rows
    .data()
    .pluck.moment.duration('Heures').asSeconds()
            .sum();
    Hours  = moment.duration(Hours).format('HH:mm');

for the Salary, this works

var Salary = rows
.data()
.pluck.('Salary')
.sum();
                 Salary = $.fn.dataTable.render.number('’', '.', 2, '', ' CHF').display(Salary); 

Thanks for your kind support.
Cheers
Philippe

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    I'd suggest using Moment.js, as it's excellent for all time/date based operations.

    Colin

  • PhilouPhilou Posts: 24Questions: 4Answers: 0

    I agree with, you but unfortunately, if you look the code I don't use it right.....

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    Ah, I missed that. We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • allanallan Posts: 63,210Questions: 1Answers: 10,415 Site admin

    I presume you are using this sum() method? If so, then yes that won’t work since it is only designed for numbers and doesn’t know anything about dates or times.

    You’d need to get the array of times (using the pluck() method as you are) and then loop over them, either parsing then yourself into integers (seconds) or use Moment to do it. See the Moment documentation for how to do that.

    Allan

  • PhilouPhilou Posts: 24Questions: 4Answers: 0

    Hi Allan,
    Thanks for your kind reply.
    I solve it nearly like you described it.
    An other problem to solved was, that I am looking for to sum the hours, to know for example how many hours people did work on a project. The format style like "hh:mm" provide by "moment.js reset every 24:00 hours, so, it doesn't work if you did work 1000 hours on a project. I didn't found out in moment how to change "hh:mm:ss" into milliseconds so I did it on server side. May be you can tell me because the manual of moment is quit long...... anyway I combined moment.js and Moment Duration Format v2.2.2 which complete moment.js to get this :

    var Milliseconds = api
                .column( 1 ,{ search: 'applied' })
                .data()
        .sum();
        Milliseconds = moment.duration(Milliseconds,'seconds');
        Hours = moment.duration(Milliseconds).format('h [hrs], m [min]', {userLocale: "de-CH"});
    

    May be it can help you too. You can even get the country format.

    Cheers

    Philippe

  • PhilouPhilou Posts: 24Questions: 4Answers: 0

    This is also working :

    var Sec = rows
    .data()
    .pluck( 'Heures_en_Seconde')
    .sum();
    Milliseconds = moment.duration(Sec,'seconds');
    Hours =moment.duration(Milliseconds).format('h [hrs], m [min]', {userLocale: "de-CH"});

    but I have still a question.
    If you use .pluck, would something like this work with the right syntax :smile:

    .pluck( moment('Heures_en_Seconde').format("hh:mm:ss","ssss"))

This discussion has been closed.