Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,211
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the reply, the problem is solved,

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,633
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,211
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    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.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,633
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,211
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •