Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7037885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:35:57+00:00 2026-05-28T01:35:57+00:00

I have an algorithim where it calculates the total duration remaining. The format of

  • 0

I have an algorithim where it calculates the total duration remaining. The format of the duration is ’00 Hrs 00 Mins 00 Secs’. How the calculation works is like this for example:

if total duration remaning is 01 Hrs 30 Mins 20 Secs and user enters in duration 00 Hrs 50 Mins 00 Secs and adds the row, then 01 Hrs 30 Mins 20 Secs minus 00 Hrs 50 Mins 00 Secs will make new total duration remaining equal 00 Hrs 40 Mins 20 Secs.

If the total duration goes into negative then format goes like this : – 00 Hrs 00 Mins 00 Secs

I have a couple of problems though:

Problem 1:
Lets say total duration remaining is ’00 Hrs 00 Mins 50 Secs’. Then if I add 00 Hrs 00 Mins 50 Secs twice, the total duration remaining should equal ‘- 00 Hrs 00 Mins 50 Secs’

But instead it is displaying this ‘ – 00 Hrs 01 Mins 0-10 Secs’.

Problem 2:
Lets say total duration remaining is ’00 Hrs 50 Mins 00 Secs’. Then if I add 00 Hrs 50 Mins 00 Secs twice, the total duration remaining should equal ‘- 00 Hrs 50 Mins 00 Secs’

But instead it is displaying this ‘ – 01 Hrs 0-10 Mins 00 Secs.

This is a strange problem. But I beleive this problem is happening because of my algrorithm. So my question is that does anyone know how to fix the algrorithm if the algrorithm is at fault?

Below is the code:

var format = duration.match(/(\d\d)/ig),
hours = parseInt(format[0], 10),
mins = parseInt(format[1], 10),
secs = parseInt(format[2], 10);


function calculateDuration()
{
    var totalduration = duration;  
    var sign = '';
    var tmp_hours = 0;
    var tmp_mins = 0;
    var tmp_secs = 0;

    $("#qandatbl td.duration input").each(function (){
        tmp_format = $(this).val().match(/(\d\d)/ig),
        tmp_hours += parseInt(tmp_format[0], 10),
        tmp_mins += parseInt(tmp_format[1], 10),
        tmp_secs += parseInt(tmp_format[2], 10);

    });

tmp_mins += Math.floor(tmp_secs / 60);
tmp_secs = tmp_secs % 60;
tmp_hours += Math.floor(tmp_mins / 60);
tmp_mins = tmp_mins % 60;

    newH = hours - tmp_hours;
    newM = mins - tmp_mins;
    newS = secs - tmp_secs;

    if( newS < 0 ) {
        newS += 60;
        newM--;
    }
    if( newM < 0 ) {
        newM += 60;
        newH--;
    }       

    if(newH < 0) {
        newM = Math.abs(newM - 60);
        newH = Math.abs(newH + 1);

        sign = '- ';
    }      

    checkedH = (newH < 10 ? '0' : '') + newH;
    checkedM = (newM < 10 ? '0' : '') + newM;
    checkedS = (newS < 10 ? '0' : '') + newS;

    new_duration = sign + checkedH + ' Hrs ' + checkedM + ' Mins ' + checkedS + ' Secs';


    $("#total-duration").text(new_duration);
}

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:35:58+00:00Added an answer on May 28, 2026 at 1:35 am

    updated answer

    Rewrote the algorithm to deal with seconds only

    var format = duration.match(/(\d\d)/ig),
    hours = parseInt(format[0], 10),
    mins = parseInt(format[1], 10),
    secs = parseInt(format[2], 10);
    
    function secondsFromTime(timeString){
        var format = timeString.match(/(\d\d)/ig),
            h = parseInt(format[0], 10),
            m = parseInt(format[1], 10),
            s = parseInt(format[2], 10);
    
        return (h*3600) + (m*60) + s;
    }
    
    function calculateDuration()
    {
        var totalduration = secondsFromTime(duration);
        var sign = '';
        var durationchange = 0;
    
        $("#qandatbl td.duration input").each(function (){
            durationchange += secondsFromTime(this.value);
        });
    
        sign = (totalduration < durationchange ) ? '-' : '';
        finalduration = Math.abs(totalduration - durationchange);
    
        checkedH = ('0' + Math.floor(finalduration / 3600)).slice(-2);
        checkedM = ('0' + Math.floor((finalduration % 3600) / 60)).slice(-2);
        checkedS = ('0' + Math.floor(finalduration % 60)).slice(-2);
    
        new_duration = sign + checkedH + ' Hrs ' + checkedM + ' Mins ' + checkedS + ' Secs';
    
    
        $("#total-duration").text(new_duration);
    }
    

    demo at http://jsfiddle.net/gaby/4xjcW/1/


    original answer
    Use the Math.abs to get the absolute value..

    checkedH = (Math.abs(newH) < 10 ? '0' : '') + Math.abs(newH);
    checkedM = (Math.abs(newM) < 10 ? '0' : '') + Math.abs(newM);
    checkedS = (Math.abs(newS) < 10 ? '0' : '') + Math.abs(newS);
    

    The problem is that you are returning 0 as a string and adding -10 so it becomes 0-10

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this algorithm in Java to store passwords in database. I'd like to
I have this algorithm that I want to implement on VB6. Sub Main() dim
This is a question about k-means clustering algorithm. I have the following points and
I have a small webshop-like web application, and i have big plans :-) I
I currently have some python code I'd like to port to C++ as it's
I have a numerical analysis program which for simplicity calculates an algorithm similar to:
I have this situation that I need to let users define decisions based on
Example: I have 100 samples for some certain time period. But I can use
I have a website where users can Like and Dislike items. So for each
I have an algorithm that calculates the percentile(85) with Apache Commons of a series

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.