1

I have seen few post in here about this issue but I couldn't find the one as my case. So I have a table with three columns say column A, B and C where A and B includes dates in the form of "3/9/2016 4:28:42 PM". Column C is the difference of columns A and B. I got the difference but in milliseconds. I need each records of the column in days: hours: min format. I have seen one post Javascript show milliseconds as days:hours:mins without seconds but couldn't implement in my loop. Here is the JavaScript I am working on. How do I do it?

    ' SQL = some statements

    <script>
        $(function() {
            ShowDifference();
            ShowAverage();
        });
         function ShowDifference() {
            var assigned; 
            var completed;
            var difference;
            $('#tbl tr').each(function() {
            assigned = new Date($(this).find('.assigned').html())
            completed = new Date($(this).find('.completed').html())
            difference = completed - assigned;
               // $(this).find('.difference').html(difference/1000/60/60);
            $(this).find('.difference').html(difference);
            });
        }
        function ShowAverage() {
           var difference = Number(0);
           var countRows = Number(0);
           var average;
           $('#tbl tr .difference').each(function() {
               countRows = ($(this).html() !== '') ? countRows + 1 : countRows;
               difference += Number($(this).html());
           });
           average = Number(difference / countRows);
           $("#tbla #average").html(average);
           }
    </script>        
Community
  • 1
  • 1
VicStat
  • 47
  • 1
  • 10

2 Answers2

5

Convert to seconds (by dividing by 1000), then convert to hours (by dividing by 3600) and the remainder divide by 60 to get minutes.

All calculations are being done on the total seconds so in comments i describe what is being used for the calculation

function ShowDifference() {
    var assigned; 
    var completed;
    var difference;

    $('#tbl tr').each(function() {
        assigned = new Date($(this).find('.assigned').html())
        completed = new Date($(this).find('.completed').html())
        difference = completed - assigned;

        var totalSeconds = difference / 1000;
        var days = Math.floor(totalSeconds / 86400); // 86400 = 24 hours * 60 minutes * 60 seconds per day
        var hours = Math.floor((totalSeconds % 86400) / 3600); // 3600 = 60 minutes * 60 seconds per day
        var minutes = Math.floor((totalSeconds % 3600) / 60); // 60 = 60 seconds per minute

        // $(this).find('.difference').html(difference/1000/60/60);
        $(this).find('.difference').html(days + ":" + hours + ':' + minutes);
    });
}
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • added days as well, as i had not noticed you wanted that too – Gabriele Petrioli Mar 10 '16 at 18:05
  • Added `Math.floor` to the results to remove decimals. – Gabriele Petrioli Mar 10 '16 at 18:24
  • Thanks alot. However, I got "NaN:NaN:NaN" for empty columns. – VicStat Mar 10 '16 at 19:34
  • Also I wanted or thought to calculate average of those time difference calculated thinking it may be helpful to other people. I had added my code for calculating average and display it somewhere in another small table. However, It doesn't show the average. I think it is because the "difference" variable had changed. Can anyone tell me where did I do wrong? – VicStat Mar 10 '16 at 21:12
  • for the empty cells, just add a check beforehand and if `find('.assigned').html()` or `find('.assigned').html()` are empty ignore the rest of the code. – Gabriele Petrioli Mar 10 '16 at 22:17
1

Just place 1439198499 with your difference.

Here is the jsfiddle link: jsfiddle

I use console for the output.

Try this:

var date = new Date(1439198499*1000);

// Days part from the timestamp
var days = date.getDay();

// Hours part from the timestamp
var hours = date.getHours();
// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();


// Will display time in 10:30:23 format
var formattedTime = days + ':' + hours + ':' + minutes.substr(-2);
console.log(formattedTime);

Output

1:15:21

Murad Hasan
  • 9,565
  • 2
  • 21
  • 42