-2

I have 2 timestamps

var startTimestamp = 1488021704531;
var endTimestamp = 1488022516572;

I need the difference between these timestamps in hours and minutes using javascript but without using moment.js. Means the output should in hours and minutes like for ex:(02h 13min).

prince
  • 33
  • 3
  • First convert it to date object something , new Date(1488022516572) etc after that write your logic – Shobhit Walia Feb 26 '17 at 09:03
  • 1
    This is done with basic math. StackOverflow is not a code writing service or a tutorial service. Please explain problems you are having achieving your goal and show the code you have that isn't working as expected – charlietfl Feb 26 '17 at 09:21
  • @prince - please provide some more context or in depth information of the solution you have up to now. That said, I understand the need/question of your second part "hours/minutes/seconds". – Yves Schelpe Feb 26 '17 at 09:39
  • 1
    Possible duplicate of [Calculate difference between 2 timestamps using javascript](http://stackoverflow.com/questions/16767301/calculate-difference-between-2-timestamps-using-javascript) – Bourbia Brahim Feb 26 '17 at 10:11
  • @bRIMOs I already tried the solution given in your link but it didnt work it gives not the correct result – prince Feb 26 '17 at 10:53

1 Answers1

0

Do provide some more context or in depth information of the solution you have up to now. That said, I understand the need/question of your second part "hours/minutes/seconds"; below is some context on that, or read up on it at milliseconds to time in javascript.

That being said,

You could just either try subtracting, as in end - start.. as in following code example.

    var startTimestamp = 1488021704531;
    var endTimestamp = 1488022516572;
    document.write(endTimestamp - startTimestamp + '<br/>');

This will output 812041 - which are the milliseconds.

If you want to convert those milliseconds to the known format of hh:mm:ss.ms you can try the following code by example - also on jsfiddle.

    var startTimestamp = 1488021704531;
    var endTimestamp = 1488022516572;
    document.write(endTimestamp - startTimestamp + '<br/>');
    document.write(millisecondsToHoursMinutesSeconds(endTimestamp - startTimestamp));
    
    document.write('<hr/>');
    
    function millisecondsToHoursMinutesSeconds(ms) {
        var milliseconds = parseInt((ms%1000)/100)
            , seconds = parseInt((ms/1000)%60)
            , minutes = parseInt((ms/(1000*60))%60)
            , hours = parseInt((ms/(1000*60*60))%24);
    
        hours = (hours < 10) ? "0" + hours : hours;
        minutes = (minutes < 10) ? "0" + minutes : minutes;
        seconds = (seconds < 10) ? "0" + seconds : seconds;
    
        return hours + ":" + minutes + ":" + seconds + "." + milliseconds;
    }

Or read other solutions in this question: milliseconds to time in javascript

Community
  • 1
  • 1
Yves Schelpe
  • 3,343
  • 4
  • 36
  • 69
  • 1
    for the ones downvoting, it wouldn't hurt mentioning **why**. Helping out users is the main reason for this site, sure you can tell @prince to provide a bit more clarity, info on what he tried and what his code currently is doing etc - but you can slo try and help people out afterwards with some links & pointers in stead of leaving them in the cold. And if my answer is incorrect, do edit it or post **another** answer. I have no problem with that, silent downvotes just don't "add value". – Yves Schelpe Feb 26 '17 at 09:43
  • 1
    Added a +1. The answer in my vision is how it is supposed to be. Hopefully, it'll cancel out up to 5 downvotes. – Tariq B. Feb 26 '17 at 10:19