...

View Full Version : using unix time stamp in Javascript



vdevil
12-06-2012, 07:29 PM
Hi All,
I am using some values from a database in unix timestamp format, i want to use those values in Javascript to compare with the current time , e.g. i am using


var ts = Math.round((new Date()).getTime() / 1000);
for loop
{
if ( ts > unixtimestamp value)
a=b
}

but it is not working, please guide.

Old Pedant
12-06-2012, 07:42 PM
Show some REAL CODE.

Or show a real website, even better.

That code you showed is both meaningless and very bad, as you would NEVER want to run a loop like that in JavaScript!

vdevil
12-10-2012, 06:07 PM
thanks for the reply, the problem is solved,

felgall
12-10-2012, 09:12 PM
Since the OP didn't post their solution I'll provide one for anyone looking at how to load a unixtimestamp into JavaSScript.

var dt = new Date(unixtimestamp);

A unistimestamp is one of the valid formats that can be used to set the initial value for a JavaScript Date object.

Old Pedant
12-10-2012, 10:24 PM
Since the OP didn't post their solution I'll provide one for anyone looking at how to load a unixtimestamp into JavaSScript.

var dt = new Date(unixtimestamp);

A unistimestamp is one of the valid formats that can be used to set the initial value for a JavaScript Date object.

Ummm....are you sure on that?

How would JS tell the difference, then, between a Unix timestamp (which is in *seconds*) and a JavaScript Date.getTime() value (which is in millliseconds).

That is:


var unixtime = 1357027200;
var d1 = new Date(unixtime);
var d2 = new Date(unixtime*1000);
document.write( d1 + "<br/>" + d2 );

Produces:


Fri Jan 16 1970 08:57:07 GMT-0800 (Pacific Standard Time)
Tue Jan 01 2013 00:00:00 GMT-0800 (Pacific Standard Time)

Just a tiny bit of difference.

I think you meant to say that

var dt = new Date(unixtimestamp * 1000);

will do the job??

felgall
12-11-2012, 01:18 AM
I think you meant to say that

var dt = new Date(unixtimestamp * 1000);

will do the job??

You're right - I had forgotten that the timestamps are in seconds while JavaScript expects milliseconds.

Philip M
12-11-2012, 10:53 AM
<script type = "text/javascript">

var unixtime = 1357027200;
var d = new Date(unixtime*1000).toString()
alert (d);

</script>

Old Pedant
12-11-2012, 09:04 PM
Okay, Philip, how is that different than my post #5??



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum