SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)

    Convert getTimezoneOffset(); into Standard GMT Statement

    Hi.

    I'm using Javascript's getTimezoneOffset() to get the client's current Timezone but of course this only returns the Timezone in minutes and doesn't tell you if it's more or less than Greenwich Meridian Time.

    Converting the minutes to hours is easy enough... just divide by 60... but how do I figure out if there should be a plus or a minus sign (more or less than GMT) before the output ?


    Code JavaScript:
    var currentTime = new Date();
    var currentTimezone = currentTime.getTimezoneOffset();
     
    currentTimezone = (currentTimezone/60);

    Any ideas ?




    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It should return a positive number if you're behind GMT and a negative one if you're after it (which seems odd, surely it should be the other way around). So, you would just do this:
    Code Javascript:
    var currentTime = new Date();
    var currentTimezone = currentTime.getTimezoneOffset();
    currentTimezone = (currentTimezone/60) * -1;
    var gmt = 'GMT';
    if (currentTimezone !== 0) {
      gmt += currentTimezone > 0 ? ' +' : ' ';
      gmt += currentTimezone;
    }
    alert(gmt); // For Trinidad this would be something like 'GMT -4' (I'm guessing there)

  3. #3
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles
    For Trinidad this would be something like 'GMT -4' (I'm guessing there)
    Correct. You know you're stuff.

    Code Javascript:
    var currentTime = new Date();
    var currentTimezone = currentTime.getTimezoneOffset();
    currentTimezone = (currentTimezone/60) * -1;
    var gmt = 'GMT';
    if (currentTimezone !== 0) {
      gmt += currentTimezone > 0 ? ' +' : ' ';
      gmt += currentTimezone;
    }
    alert(gmt); //

    I don't understand the part about multiplying by -1 ... wouldn't that give you a negative number regardless ?

    Therefore if someone on Australia ran this wouldn't they get a negative GMT too (whereas they should be positive) ?


    Edit:

    Ore wait no.

    I figured it out.

    Thanks, man.
    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  4. #4
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Another question.

    This is something I thought would have been obvious but it wasn't...

    How do I get a figure to a decided number of places before the decimal point ?


    Example (in this case)... how can I convert "4" to "0400"


    Edit:

    Which is actually four hundred but it seems to be the standard way of displaying Timezones for whatever reason.


    I played with decimals but that only gives me places after the decimal point.

    Therefore I was able to get... "4.0000" which isn't very helpful (but funny when I realised how silly it was to think it'd work. Goes to show how it's been since I've been in a math class).




    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Just manipulate it as a string. Nobody is going to be at more than GMT +/-12, so:
    Code Javascript:
    var hour = 9;
    hour = String(hour);
    if (hour.length === 1) hour = '0' + hour;
    hour += '00';

  6. #6
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Sort of cheating but hell it works !

    Thanks a lot, Raf'.






    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  7. #7
    SitePoint Member
    Join Date
    May 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Had same issue just found out the solution, thanks to you!

  8. #8
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    What how to take care of DST for example CST now days is -5.00 but after Day light saving its -6.00.

    Is there any code.

  9. #9
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The timezone offsets in JavaScript are in munites west of GMT so that timezones east of GMT have a negative sign on the front and you need to divide by 60 to get hours. Timezones actually run from 12 hours west to 14 hours east of GMT (when you take daylight saving into account).

    JavaScript doesn't provide a daylight saving flag but you can calculate one provided that the location isn't on daylight saving for the whole year.

    The one thing about timezones and daylight saving that JavaScript cannot access or calculate it the timezone name.

    Take a look at http://javascript.about.com/library/bldateformat.htm for a date method that I wrote for JavaScript that can easily produce date and time information formatted however you want it formatted with a single call.
    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="^$">


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
  •