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 8340925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:19:05+00:00 2026-06-09T05:19:05+00:00

I would like to calculate time difference based on dynamic id from 2 text-fields

  • 0

I would like to calculate time difference based on dynamic id from 2 text-fields and display it on third.

My current code:

function mdiff(t1,t2) { 
    var t1 = hour2mins(t1);  var t2=hour2mins(t2); 
    var ret = mins2hour(parseInt(t2-t1));
  if(t2<t1) {
     ret=mins2hour(parseInt(parseInt(t2+1440)-t1));
  } 
    return ret; 
}

$("input.[rel=diff]").keyup(function (b){ 
   $("#duration").val(mdiff($("#starttime).val(),$("#endtime").val())); 
});

I am using trent’s timepicker addon in my code to generate time. How can I calculate the difference?

fiddle of my existing code

  • 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-06-09T05:19:07+00:00Added an answer on June 9, 2026 at 5:19 am

    You can use the ‘onSelect’ event for the ‘timepicker’ Object like in the example below:

    $(document).ready(function() {
    
        $("#AddRow").click(function() {
            var row = '<tr><td>' + '<input type=text class="timepicker"  value=""/></td>' + '<td><input type=text class="timepicker" value=""/></td>' + '<td><input type=text class="diff"/></td>' + '<td><button>delete</button>' + '</td></tr>';
            $("#table").append(row);
    
            // id generating for class timepicker starts here
            var i=0;
            $('input.timepicker').each(function(){
                i++;
                $(this).attr("id","timepicker_"+i);
            });
            // ends here
            //trail diff id generator
            var j=0;
            $('input.diff').each(function(){
                j++;
                $(this).attr("id","diff_"+j);
            });
            //ends here
    
        });
    
        $('body').on('focus', ".timepicker", function() {
            $(this).timepicker({
    
                onSelect: function(dateText, instance) {
    
                    // Check for valid values
                    if ($('input.timepicker:eq(0)').val() === '' || $('input.timepicker:eq(1)').val() === '') return false;
    
                    var time1 = $('input.timepicker:eq(0)').val().split(/:/),
                        time2 = $('input.timepicker:eq(1)').val().split(/:/);                                      
    
                        timeDiff(time1[0]*3600 + time1[1]*60, time2[0]*3600 + time2[1]*60);
                }
            });
        });
    
        $("#table").on("click", "button", function() {
            $(this).closest("tr").remove();
        });
        //table structure ends here
    });
    
    function timeDiff(time1, time2) {
        var td = time2 - time1,
            hours = parseInt(td / 3600),
            minutes = parseInt( (td - hours*3600) / 60 ),
            diff = ( (hours < 10 && hours >= 0) ? ('0' + hours) : hours ) + ':' + ( (minutes < 10 && minutes >= 0) ? ('0' + minutes) : minutes );
    
        $('input.diff').val(diff);
    }
    

    Yes, that’s true. I missed the ‘Add’ button and these selectors were meant to work for one row. Here is an update of the previous code:

    $(document).ready(function() {
    
        $("#AddRow").click(function() {
            var row = '<tr><td>' + '<input type=text class="timepicker"  value=""/></td>' + '<td><input type=text class="timepicker" value=""/></td>' + '<td><input type=text class="diff"/></td>' + '<td><button>delete</button>' + '</td></tr>';
            $("#table").append(row);
    
            // id generating for class timepicker starts here
            var i=0;
            $('input.timepicker').each(function(){
                i++;
                $(this).attr("id","timepicker_"+i);
            });
            // ends here
            //trail diff id generator
            var j=0;
            $('input.diff').each(function(){
                j++;
                $(this).attr("id","diff_"+j);
            });
            //ends here
    
        });
    
        $('body').on('focus', ".timepicker", function() {
            $(this).timepicker({
    
                onSelect: function(dateText, instance) {
    
    
    
                    var currentInput = ( $(instance.input).length > 0 ? $(instance.input) : $(instance.$input)),
                        nextInput = $(currentInput).closest('tr').find('input.timepicker[id!="' + $(currentInput).attr('id') + '"]');
    
                    // Check for valid values               
                    if ($(currentInput).val() === '' || $(nextInput).val() === '') return false;
    
                    var time1 = $(currentInput).val().split(/:/),
                        time2 = $(nextInput).val().split(/:/);
    
                    // switch time
                    if ($(currentInput).parents('td').index() > 0) {
                        var temp = time1;
                        time1 = time2;
                        time2 = temp;
                    }
    
                        timeDiff(time1[0]*3600 + time1[1]*60, time2[0]*3600 + time2[1]*60, currentInput);
                }
            });
        });
    
        $("#table").on("click", "button", function() {
            $(this).closest("tr").remove();
        });
        //table structure ends here
    });
    
    function timeDiff(time1, time2, instance) {
        var td = time2 - time1,
            hours = parseInt(td / 3600),
            minutes = parseInt( (td - hours*3600) / 60 ),
            diff = ( (hours < 10 && hours >= 0) ? ('0' + hours) : hours ) + ':' + ( (minutes < 10 && minutes >= 0) ? ('0' + minutes) : minutes );
    
        $(instance).closest('tr').find('input.diff').val(diff);
    }
    

    UPDATE: — “is there a way to make this code work when users enters time manually ?”

    Inside $(document).ready(); you’ll have to add an ‘onblur’ event for the timepicker input.
    Here is an example: jsFiddle

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

Sidebar

Related Questions

I would like to calculate a timestamp based on the input date. I would
I would like to calculate a density function of a distribution whose characteristics function
I would like to calculate total order amount in the previous month. I got
I would like to calculate my total order amount in the previous week. I
I would like to calculate the similarity between users, which is reciprocal. similarity[:user1][:user2] ==
Lets say I would like to calculate percents and do: double n = ...;
I have a matrix mat and would like to calculate the mean of the
I'm developing an AI sandbox and I would like to calculate what every living
Good morning, I have mysql queries where I would like to calculate percentage of
I would like to be able to calculate the family relationship between two individuals

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.