Results 1 to 12 of 12

Thread: javascript's date() and the unix timestamp

  1. #1
    satis's Avatar
    Join Date
    Oct 2002
    Location
    Dallas
    Posts
    2,914
    Rep Power
    20

    javascript's date() and the unix timestamp

    Is there some way to convert a Unix timestamp into a javascript date() object or vice versa? Basically I currently have a unix timestamp available to js and I need to see whether that timestamp refers to today. If I can either get the unix timestamp into a date object or the reverse, I can figure out the rest myself.

    My alternative would be to go back into the PHP side of things and squirt a non-unix timestamp into js, but that's dirty. The unix timestamp is already available so I'd prefer to use that.

    Some googling left me more confused than anything.

  2. #2
    HalfaBee's Avatar
    Join Date
    Feb 2002
    Location
    Sydney, Australia
    Posts
    7,266
    Rep Power
    26
    Maybe something like this?

    var newDate = new Date( );
    newDate.setTime( unixtime*1000 );
    dateString = newDate.toUTCString );
    I don't suffer from laziness, I enjoy every minute!
    Edit your php.ini here
    http://members.powweb.com/member/cgi...nt/PHPplus.bml

  3. #3
    satis's Avatar
    Join Date
    Oct 2002
    Location
    Dallas
    Posts
    2,914
    Rep Power
    20
    oh... so the javascript epoch is equivalent to the unix epoch? I guess that does make things easier. I'll play with that, thanks.

  4. #4
    satis's Avatar
    Join Date
    Oct 2002
    Location
    Dallas
    Posts
    2,914
    Rep Power
    20
    thought I'd just post back to say that Halfa was on the money. Here's a code snippet showing it in action (sorta).

    PHP Code:
    var today = new Date();
    var 
    day = new Date();
    day.setTime(el.id 1000);
    if(
    today.getTime() >= day.getTime() && today.getTime() < (day.getTime() + (60*60*24*1000))){
        var 
    hour today.getHours();
        var 
    meridiem = (hour 12)? 'am' 'pm';
        
    hour = (hour 13)? hour hour 12;
        var 
    minutes = (today.getMinutes() < 10)? '0' today.getMinutes() : today.getMinutes();
        var 
    baseTime hour ':' minutes meridiem;

    That's obviously only a part. el.id was set to the unix time referring to 12am of the day.

  5. #5
    You are what you is... troycawley's Avatar
    Join Date
    Nov 2006
    Location
    Wildwood, NJ
    Posts
    245
    Rep Power
    11
    Thanks for posting back. That is good information W/R/T times in PHP and JS.

    But just avoid confusion: I think you meant Javascript code (not PHP) above, yes?

  6. #6
    satis's Avatar
    Join Date
    Oct 2002
    Location
    Dallas
    Posts
    2,914
    Rep Power
    20
    hah...yes...that code above is javascript. I only included it in a php codeblock because it color codes it.

  7. #7
    on hiatus
    Join Date
    Aug 2008
    Location
    canda
    Posts
    4
    Rep Power
    0
    I've been using Unix since before you were born (I can tell by the fact that you haven't learned to use the SHIFT key), and have no idea what you mean by a "unix timestamp" and don't have any inclination to look at mysql documentation to see what they think it is.

    Give an example of what you want.
    Last edited by Doc C; 9-4-08 at 09:21 AM. Reason: non-PowWeb hosted site signature removed per forum policy

  8. #8
    satis's Avatar
    Join Date
    Oct 2002
    Location
    Dallas
    Posts
    2,914
    Rep Power
    20
    Wow, you sure have quite a bit of attitude for a whole lot of not posting. Please reread the thread, you'll see that the answer was already provided. Additionally, from actually reading the thread rather than jumping to conclusions, you may realize that by 'Unix Timestamp' I'm referring to the Unix epoch.

  9. #9
    Just another mook Doc C's Avatar
    Join Date
    Mar 2006
    Location
    Southern CA
    Posts
    5,098
    Rep Power
    20
    Signatures are earned and non-PowWeb hosted signatures are not allowed.
    "You don't really understand human nature
    unless you know why a child on a merry-go-round will wave at his parents every time around
    -- and why his parents will always wave back."

    -William D. Tammeus

  10. #10
    Mod.. with bite.. Croc Hunter's Avatar
    Join Date
    Sep 2002
    Location
    Australia
    Posts
    7,332
    Rep Power
    26
    Wow alright, I just got around to reading this thread - kudos to Halfabee and Satis for posting a solution, thank-you.

    amanda12: Flaming people here will usually earn you a ban no matter who you are. Satis is a very knowledgeable constructive member here. Your comments are not constructive and have earned you a two week ban from the forum. Please think twice before posting non-constructive comments in future.
    Croc Hunter MSC :

  11. #11
    You are what you is... troycawley's Avatar
    Join Date
    Nov 2006
    Location
    Wildwood, NJ
    Posts
    245
    Rep Power
    11
    Quote Originally Posted by Croc Hunter View Post
    amanda12Your comments are not constructive and have earned you a two week ban from the forum..
    What a shame, I bet we don't have anyone else from the country of "canda".

    I hope she's back soon.

  12. #12
    Just another mook Doc C's Avatar
    Join Date
    Mar 2006
    Location
    Southern CA
    Posts
    5,098
    Rep Power
    20
    Uh, the IP traces back to broadband out of India.
    "You don't really understand human nature
    unless you know why a child on a merry-go-round will wave at his parents every time around
    -- and why his parents will always wave back."

    -William D. Tammeus

Posting Permissions

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