Ben Muircroft
Ben Muircroft

Reputation: 3034

javascript unix timestamp older than x

I need to clarify an assumpton I have made with unix timestamps before I go and make something that messes my application flow, I can't visualise the numbers at all I don't wan't to do the opposite..

There is a question here about this with php but not one that I can find with javascript

How to check if a timestamp is an hour old?

My array of arrays will be built by my application like this

var list=[
['name0',1424300986716,1424301040055,1424301049633],
['name1',1424300998116,1424301019372,1424301059418],
['name2',1424301009043,1424301030315,1424301059418]
//...
];

I would like to determin if the second column of timestamps has one which is over 24 hours long old (none of these are I just made them now), I need it to get the first one that it comes across not the oldest and return the array index

if I run this test I get more confused as I am in italy I think some thing is screewy with the timezones or clock or I have the wrong idea

var now=(+new Date());

var back24hrs=new Date((now-86400));

console.log(now,back24hrs);

// 1424303535659 Thu Feb 19 2015 00:50:49 GMT+0100 (W. Europe Standard Time)

It's thursday now that is now not back 24 hrs! I can't see how I am wrong..

basicly my assumption is (current time - 24hrs) I think that part is correct but also less than or greater than the saved time?

I was thinking of doing something like this:

function olderthan24hrs(column){
    var hrs24=86400;
    var i=list.length;
    while(i--){
        if((+new Date()-hrs24)>list[i][column]){break;}
        }
    return i;
    }

var choice=olderthan24hrs(2);

But I'm really not sure if that would be at all correct

Upvotes: 0

Views: 2536

Answers (2)

RobG
RobG

Reputation: 147363

You already have an answer, but just to clarify: UNIX and JavaScript (ECMAScript) use the same epoch for time values, however UNIX time values are seconds and javascript time values are in milliseconds. There are 8.64e7 milliseconds in 24 hours.

If you want to see if the time is greater than 24 elapsed hours ago, set the comparison value by subtracting 24 hours worth of seconds form the time value in seconds:

// Current time value in seconds
var then = (Date.now()/1000 | 0) - 8.64e4;

However, the time that this relates to on the previous day will be +/- 1 hour when it crosses a daylight saving boundary.

If you want to compare to the same time yesterday (so 23 or 25 hours over the daylight saving boundary), use a date object and subtract a day to create the comparison value:

var now = new Date();
var then = +(now.setDate(now.getDate() - 1))/1000 | 0;

Then do the comparison (same for both):

if (list[i][column] < then) {
  // value is prior to comparison time
}

Note that storing the then value is much more efficient than calculating it on each iteration.

Your while loop starts:

while(i--){

So this is a decrementing loop so you'll get the last value over 24 hours old, not the first. To get the first:

for (var i=0, iLen=list.length; i<iLen; i++) {
    if ((list[i][column] < then) {
      return i;
    }
}

that way if there is no time more before the comparison time, the function returns undefined. In the OP, it will return -1 if a suitable time isn't found.

Upvotes: 1

C3roe
C3roe

Reputation: 96241

JavaScript’s Date object operates with milliseconds, not seconds.

So with 86400 you’re subtracting a mere 84.6 seconds from the current timestamp, and not “a whole day’s worth of seconds” as you think. You need to multiply that value by a thousand.

(And subtracting 86400 seconds doesn’t mean exactly one day all the time, since there are two days in the year that are one hour longer resp. shorter, at least in any country that has daylight saving time. Whether or not that is relevant for your application, you must decide yourself.)

Upvotes: 1

Related Questions