SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Why one extra hour in my countdown?

    I tried to experiment with calculating days, hours, minutes, and seconds between current time and a future time. but I found I always got one extra hour,
    does anyone know where the problem is?

    Code:
    var date = new Date("12/21/2013 00:00:00 GMT-0500");
    console.log(date);
    var now = new Date();
    console.log(nowf);
    var timeDiff = date - now;
    var days = Math.floor(timeDiff/(1000*60*60*24));
    var hours = Math.floor((timeDiff%(1000*60*60*24))/(1000*60*60));
    var minutes = Math.floor((timeDiff - (1000*60*60*24)*days - (1000*60*60)*hours)/(1000*60));
    var seconds = Math.floor((timeDiff - (1000*60*60*24)*days - (1000*60*60)*hours - (1000*60)*minutes)/1000);
    console.log(days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds!");

  2. #2
    Non-Member
    Join Date
    Feb 2012
    Posts
    892
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    date is GTB Daylight Time.
    now is GTB Standard Time.
    ... or the other way around, depending on where you're asking.

    Daylight and Standard time differ by an hour.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,826
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by myty View Post
    date is GTB Daylight Time.
    now is GTB Standard Time.
    ... or the other way around, depending on where you're asking.

    Daylight and Standard time differ by an hour.
    The extra hour implies that now is standard time and December is DST. If it were the other way around then it would be an hour less rather than an hour more.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    Non-Member
    Join Date
    Feb 2012
    Posts
    892
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    The extra hour implies that now is standard time and December is DST. If it were the other way around then it would be an hour less rather than an hour more.
    Run the code. Even if browsers are not consistent about which one is Standard and which one is Daylight, all of them give the same one extra hour difference: GMT+0100 vs. GMT+0200.

    IE10
    Sat Dec 21 06:00:00 UTC+0100 2013
    Thu Aug 21 23:44:42 UTC+0200 2013

    FF23.0.1
    Date {Sat Dec 21 2013 06:00:00 GMT+0100 (GTB Daylight Time)}
    Date {Thu Aug 21 2013 23:47:34 GMT+0200 (GTB Standard Time)}

    Op15
    Sat Dec 21 2013 06:00:00 GMT+0100 (GTB Standard Time)
    Thu Aug 21 2013 23:49:17 GMT+0200 (GTB Daylight Time)

    Ch29
    Sat Dec 21 2013 06:00:00 GMT+0100 (GTB Standard Time)
    Thu Aug 21 2013 23:50:33 GMT+0200 (GTB Daylight Time)

    Some more readup: http://stackoverflow.com/questions/1...how-many-hours.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,826
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by myty View Post
    Even if browsers are not consistent about which one is Standard and which one is Daylight, all of them give the same one extra hour difference:
    ALL browser will identify the same one as standard and the same one as daylight - which is which depends on the timezone/location the local computer is set to and has nothing to do with the browser.

    There will be three possible results for that date comparison depending on where in the world the person running the code is located.
    1. If their location is currently on standard time and will be on daylight time in December it will be an hour more than expected.
    2. If their location is currently on daylight time and December is standard time then it will be an hour less than expected.
    3. If their location does not use daylight time at all then it will not be out either way.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    Non-Member
    Join Date
    Feb 2012
    Posts
    892
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    ALL browser will identify the same one as standard and the same one as daylight - which is which depends on the timezone/location the local computer is set to and has nothing to do with the browser.
    Again, just RUN the code, ANALYZE the results.
    As you can see from the results I posted above, NOT ALL browser(s) will id the same one as Daylight. I'm not sure what your angle is arguing against the obvious...

  7. #7
    Non-Member
    Join Date
    Feb 2012
    Posts
    892
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    The extra hour implies that now is standard time and December is DST. If it were the other way around then it would be an hour less rather than an hour more.
    Quote Originally Posted by felgall View Post
    If I were to run that same code here in Australia then instead of all your results matching my number 2 point above it would instead match the number 1 point. Someone closer to the equator where they don't have daylight time it would not have the hour difference either way.
    You are correct.
    That's what I meant by "... or the other way around, depends where you're asking".

  8. #8
    Non-Member
    Join Date
    Feb 2012
    Posts
    892
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Not really, no.
    In post#3, you quoted my first post, post#2, implying I was only accounting for one scenario: northern hemisphere. I wasn't.

    date is GTB Daylight Time.
    now is GTB Standard Time.
    ... or the other way around, depending on where you're asking.

    Daylight and Standard time differ by an hour.
    Hopes this clarifies things.

    Regarding the FF bug, I thought it was a worthy mention, especially since you resorted to caps to enforce a slightly wrong point of view.

  9. #9
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,826
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    My post #3 was simply expanding on your post #2 to clarify that the way around that applied in the OP's location. I was not disagreeing with you as to that elsewhere it could be the other way around.

    The only other discrepancies that have shown up in this thread is that Firefox is reporting things backwards as to which timezone is which and you got things bacvkwards in post #8 as to which browsers are misreporting your current timezone.

    Apart from those discrepancies we are in agreement.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Tags for this Thread

Bookmarks

Posting Permissions

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