Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-06-2012, 07:29 PM   PM User | #1
vdevil
New to the CF scene

 
Join Date: Dec 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
vdevil is an unknown quantity at this point
Smile using unix time stamp in Javascript

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.

Last edited by vdevil; 12-06-2012 at 07:32 PM..
vdevil is offline   Reply With Quote
Old 12-06-2012, 07:42 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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!
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-10-2012, 06:07 PM   PM User | #3
vdevil
New to the CF scene

 
Join Date: Dec 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
vdevil is an unknown quantity at this point
thanks for the reply, the problem is solved,
vdevil is offline   Reply With Quote
Old 12-10-2012, 09:12 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 12-10-2012, 10:24 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by felgall View Post
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:
Code:
var unixtime = 1357027200;
var d1 = new Date(unixtime);
var d2 = new Date(unixtime*1000);
document.write( d1 + "<br/>" + d2 );
Produces:
Code:
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
Code:
var dt = new Date(unixtimestamp * 1000);
will do the job??
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-11-2012, 01:18 AM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Old Pedant View Post
I think you meant to say that
Code:
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 12-11-2012, 10:53 AM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Code:
<script type = "text/javascript">

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

</script>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 12-11-2012, 09:04 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Okay, Philip, how is that different than my post #5??
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:28 AM.


Advertisement
Log in to turn off these ads.