abpetkov
abpetkov

Reputation: 914

Convert UNIX timestamp difference to minutes

I have 2 dates, that I convert to UNIX timestamp - start date and confirm date. I subtract one from another and get numbers like these:

-12643, 0, 3037, 1509, -3069

Basically, what I need to do is to get the difference between the two dates in minutes, but I don't know how to convert those to minutes. The end output should be something like: -25, 13, 155

Upvotes: 6

Views: 23496

Answers (3)

NobleUplift
NobleUplift

Reputation: 6005

You just need to divide by 60. You already have the difference between the two timestamps, so none of the Date overhead above is necessary:

var diffs = new Array(-12643, 0, 3037, 1509, -3069);
for (var i = 0; i < diffs.length; i++)
    document.write(diffs[i] % 60);

Upvotes: 0

Damiaan Dufaux
Damiaan Dufaux

Reputation: 4785

Given two UNIX timestamps: a, b; you can calculate the difference between them in minutes like this:

var a = 1377005400000; //2013-07-20 15:30
var b = 1377783900000; //2013-07-29 15:45 

var dateA = new Date(a);
var dateB = new Date(b);

var dayRelativeDifference =   dateB.getHours()*60 + dateB.getMinutes()
                            - dateA.getHours()*60 - dateA.getMinutes();
//  dayRelativeDifference will be 15

var absoluteDifference    = (b-a)/60
//  absoluteDifference will be 12975000

Also have a look at http://www.w3schools.com/jsref/jsref_obj_date.asp

Upvotes: 1

Kamala
Kamala

Reputation: 79

How did you get the original numbers? I believe the standard Unix timestamps are in seconds, so you should be able to divide by 60 to get minutes. However, Date.now() in JavaScript, for example, returns milliseconds, so you'd need to divide by 60,000.

Upvotes: 1

Related Questions