-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).

5
  • First convert it to date object something , new Date(1488022516572) etc after that write your logic Commented Feb 26, 2017 at 9: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 Commented Feb 26, 2017 at 9: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". Commented Feb 26, 2017 at 9:39
  • 1
    Possible duplicate of Calculate difference between 2 timestamps using javascript Commented Feb 26, 2017 at 10:11
  • @bRIMOs I already tried the solution given in your link but it didnt work it gives not the correct result Commented Feb 26, 2017 at 10:53

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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".
Added a +1. The answer in my vision is how it is supposed to be. Hopefully, it'll cancel out up to 5 downvotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.