Help Displaying Time

Discussion in 'Javascript' started by Melvin G. Sheppers, Jan 16, 2006.

  1. I want to display the time in a particular timezone (US Central) regardless
    where the computer is located. I have written the script below which
    displays the time in the Eastern, Central, Mountain, Pacific and Alaska time
    zones in the US. It works okay if the computer is located in the Central
    timezone but not if it is located elsewhere. I know how to get the UTC time
    and the timezoneOffset but I can't figure out how to get from there to
    Central time. Can anyone offer any suggestions?

    <SCRIPT LANGUAGE="JavaScript">

    var running = false
    var timerID= null
    var dayName=new Array(7)
    dayName[0]="Sun"
    dayName[1]="Mon"
    dayName[2]="Tue"
    dayName[3]="Wed"
    dayName[4]="Thu"
    dayName[5]="Fri"
    dayName[6]="Sat"

    function countDown() {
    var now=new Date()
    Present=now.getTime()

    running = true
    var theDay=now.getDay()
    var theDisplayDay = dayName[theDay]
    var theHour=now.getHours()
    var theDisplayHour=theHour
    var theMin=now.getMinutes()
    var theSec=now.getSeconds()
    document.forms[0].displayday.value= theDisplayDay
    document.forms[0].displayhour.value= theDisplayHour+1
    document.forms[0].displaymin.value= theMin
    document.forms[0].displaysec.value= theSec
    document.forms[1].displayday.value= theDisplayDay
    document.forms[1].displayhour.value= theDisplayHour
    document.forms[1].displaymin.value= theMin
    document.forms[1].displaysec.value= theSec
    document.forms[2].displayday.value= theDisplayDay
    document.forms[2].displayhour.value= theDisplayHour-1
    document.forms[2].displaymin.value= theMin
    document.forms[2].displaysec.value= theSec
    document.forms[3].displayday.value= theDisplayDay
    document.forms[3].displayhour.value= theDisplayHour-2
    document.forms[3].displaymin.value= theMin
    document.forms[3].displaysec.value= theSec
    document.forms[4].displayday.value= theDisplayDay
    document.forms[4].displayhour.value= theDisplayHour-3
    document.forms[4].displaymin.value= theMin
    document.forms[4].displaysec.value= theSec
    if (running) {
    timerID=setTimeout("countDown()",1000)
    }
    }
    function stopTimer() {
    clearTimeout(timerID)
    running=false
    }

    </SCRIPT>
    Melvin G. Sheppers, Jan 16, 2006
    #1
    1. Advertising

  2. Melvin G. Sheppers

    Guest

    I think there is a function setTimeZoneOffset() of the Date object that
    lets you set hours of offset from GMT (general mean time i.e. greenwich
    england)
    , Jan 16, 2006
    #2
    1. Advertising

  3. Melvin G. Sheppers

    mick white Guest

    Melvin G. Sheppers wrote:

    > I want to display the time in a particular timezone (US Central) regardless
    > where the computer is located. I have written the script below which
    > displays the time in the Eastern, Central, Mountain, Pacific and Alaska time
    > zones in the US. It works okay if the computer is located in the Central
    > timezone but not if it is located elsewhere. I know how to get the UTC time
    > and the timezoneOffset but I can't figure out how to get from there to
    > Central time. Can anyone offer any suggestions?
    >


    If you know the TimezoneOffset, and you know that CT is -6GMT (ignoring
    DST), and that some locations' offsets include fractions of an hour
    (this will effect the hrs and mins), you may compute CT.

    now=new Date();
    now.setHours(now.getHours()+(now.getTimezoneOffset()/60)-6);



    Mick


    > <SCRIPT LANGUAGE="JavaScript">
    >
    > var running = false
    > var timerID= null
    > var dayName=new Array(7)
    > dayName[0]="Sun"
    > dayName[1]="Mon"
    > dayName[2]="Tue"
    > dayName[3]="Wed"
    > dayName[4]="Thu"
    > dayName[5]="Fri"
    > dayName[6]="Sat"
    >
    > function countDown() {
    > var now=new Date()
    > Present=now.getTime()
    >
    > running = true
    > var theDay=now.getDay()
    > var theDisplayDay = dayName[theDay]
    > var theHour=now.getHours()
    > var theDisplayHour=theHour
    > var theMin=now.getMinutes()
    > var theSec=now.getSeconds()
    > document.forms[0].displayday.value= theDisplayDay
    > document.forms[0].displayhour.value= theDisplayHour+1
    > document.forms[0].displaymin.value= theMin
    > document.forms[0].displaysec.value= theSec
    > document.forms[1].displayday.value= theDisplayDay
    > document.forms[1].displayhour.value= theDisplayHour
    > document.forms[1].displaymin.value= theMin
    > document.forms[1].displaysec.value= theSec
    > document.forms[2].displayday.value= theDisplayDay
    > document.forms[2].displayhour.value= theDisplayHour-1
    > document.forms[2].displaymin.value= theMin
    > document.forms[2].displaysec.value= theSec
    > document.forms[3].displayday.value= theDisplayDay
    > document.forms[3].displayhour.value= theDisplayHour-2
    > document.forms[3].displaymin.value= theMin
    > document.forms[3].displaysec.value= theSec
    > document.forms[4].displayday.value= theDisplayDay
    > document.forms[4].displayhour.value= theDisplayHour-3
    > document.forms[4].displaymin.value= theMin
    > document.forms[4].displaysec.value= theSec
    > if (running) {
    > timerID=setTimeout("countDown()",1000)
    > }
    > }
    > function stopTimer() {
    > clearTimeout(timerID)
    > running=false
    > }
    >
    > </SCRIPT>
    >
    >
    mick white, Jan 16, 2006
    #3
  4. Melvin G. Sheppers

    Guest

    How do they have fractions of an hour? Timezones are solid hour by
    hour last time I checked.
    , Jan 16, 2006
    #4
  5. Melvin G. Sheppers

    Lee Guest

    said:
    >
    >How do they have fractions of an hour? Timezones are solid hour by
    >hour last time I checked.


    The world is a very big place.
    If I ever get out of here, I’m going to katmandu.
    Lee, Jan 17, 2006
    #5
  6. Melvin G. Sheppers

    mick white Guest

    wrote:

    > How do they have fractions of an hour? Timezones are solid hour by
    > hour last time I checked.
    >


    St. John's, Newfoundland and Labrador, Canada
    UTC/GMT -3:30 hours

    New Delhi, India
    UTC/GMT +5:30 hours

    South Australia, Australia
    UTC/GMT +10:30 hours

    To name a few.
    Mick
    mick white, Jan 17, 2006
    #6
  7. Melvin G. Sheppers

    Guest

    Weird, never heard of that.
    , Jan 17, 2006
    #7
  8. Melvin G. Sheppers

    John Bokma Guest

    John Bokma, Jan 17, 2006
    #8
  9. Melvin G. Sheppers

    John Bokma Guest

    John Bokma, Jan 17, 2006
    #9
  10. Melvin G. Sheppers

    RobG Guest

    Melvin G. Sheppers wrote:
    > I want to display the time in a particular timezone (US Central) regardless
    > where the computer is located. I have written the script below which
    > displays the time in the Eastern, Central, Mountain, Pacific and Alaska time
    > zones in the US. It works okay if the computer is located in the Central
    > timezone but not if it is located elsewhere. I know how to get the UTC time
    > and the timezoneOffset but I can't figure out how to get from there to
    > Central time. Can anyone offer any suggestions?


    You may find useful information here:

    <URL:http://www.merlyn.demon.co.uk/js-date0.htm#Tinf>


    > <SCRIPT LANGUAGE="JavaScript">


    The language attribute is deprecated, type is required:

    <SCRIPT type="text/javascript">


    >
    > var running = false
    > var timerID= null
    > var dayName=new Array(7)
    > dayName[0]="Sun"
    > dayName[1]="Mon"
    > dayName[2]="Tue"
    > dayName[3]="Wed"
    > dayName[4]="Thu"
    > dayName[5]="Fri"
    > dayName[6]="Sat"


    It's simpler to initialise the array as:

    var dayName = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];

    Ending statements with semi-colons is not mandatory, but considered good
    practice anyway.


    >
    > function countDown() {
    > var now=new Date()
    > Present=now.getTime()
    >
    > running = true
    > var theDay=now.getDay()
    > var theDisplayDay = dayName[theDay]
    > var theHour=now.getHours()
    > var theDisplayHour=theHour
    > var theMin=now.getMinutes()
    > var theSec=now.getSeconds()
    > document.forms[0].displayday.value= theDisplayDay
    > document.forms[0].displayhour.value= theDisplayHour+1


    Your day name will be wrong between 2300hrs and midnight, you have to
    get the day after modifying the time..


    > document.forms[0].displaymin.value= theMin
    > document.forms[0].displaysec.value= theSec
    > document.forms[1].displayday.value= theDisplayDay
    > document.forms[1].displayhour.value= theDisplayHour
    > document.forms[1].displaymin.value= theMin
    > document.forms[1].displaysec.value= theSec
    > document.forms[2].displayday.value= theDisplayDay
    > document.forms[2].displayhour.value= theDisplayHour-1
    > document.forms[2].displaymin.value= theMin
    > document.forms[2].displaysec.value= theSec
    > document.forms[3].displayday.value= theDisplayDay
    > document.forms[3].displayhour.value= theDisplayHour-2
    > document.forms[3].displaymin.value= theMin
    > document.forms[3].displaysec.value= theSec
    > document.forms[4].displayday.value= theDisplayDay
    > document.forms[4].displayhour.value= theDisplayHour-3
    > document.forms[4].displaymin.value= theMin
    > document.forms[4].displaysec.value= theSec


    That could be done much more efficiently in a loop:

    Something based on the following may suit:


    <script type="text/javascript">

    var dayNames = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
    var monNames = ["Jan","Feb","Mar","Apr","May","Jun",
    "Jul","Aug","Sep","Oct","Nov","Dec"];

    // Add zone acronyms and offsets here
    var timeZones = {PST:-8, GMT:0, CET:+1, ACST:+6.5, AEST:+10};

    // Return a date object offset by tzOffset
    function offsetDateObj(dateObj, tzOffset)
    {
    return new Date(dateObj.getTime() + tzOffset*3.6e6);
    }

    // Add leading zero to numbers less than 10, return a string
    function addZ(x){
    return (x<10)? '0' + x : '' + x;
    }

    // Prefix numbers with sign(+/-), return a string
    function addS(x){
    return (x>0)? '+' + x : (x==0)? '' + x : '-' + Math.abs(x);
    }

    function doDates()
    {
    var nowUTC = new Date();
    nowUTC.setTime(nowUTC.getTime()+nowUTC.getTimezoneOffset()*6e4);

    var t, times=[];
    for (var tz in timeZones){
    t = offsetDateObj(nowUTC, timeZones[tz]);
    times[times.length] = dayNames[t.getDay()] + ' '
    + addZ(t.getDate()) + ' '
    + monNames[t.getMonth()] + ', '
    + t.getFullYear() + ' '
    + addZ(t.getHours()) + ':'
    + addZ(t.getMinutes()) + ':'
    + addZ(t.getSeconds()) + ' '
    + tz + ' (GMT ' + addS(timeZones[tz]) + ')';
    }
    alert(times.join('\n'));
    }

    doDates();

    </script>


    [...]


    --
    Rob
    RobG, Jan 17, 2006
    #10
  11. Melvin G. Sheppers

    Evertjan. Guest

    Melvin G. Sheppers wrote on 17 jan 2006 in comp.lang.javascript:

    > I want to display the time in a particular timezone (US Central)
    > regardless where the computer is located.


    <script type="text/javaScript">

    var centralOffset = -5 // from UTC, right?

    function CentralTime(){
    d = new Date();
    d.setHours(d.getHours()+
    (d.getTimezoneOffset()/60)+centralOffset)
    return "Central time is "+
    two(d.getHours())+":"+
    two(d.getMinutes())+":"+
    two(d.getSeconds())+" on "+
    d.getYear()+"/"+
    (1+d.getMonth())+"/"+
    d.getDate();
    }

    function two(x){
    return (+x>9)?(''+x):('0'+x)
    }


    alert(CentralTime())

    </script>

    =====================

    This is not regardless of the local computer's setting mistakes!!!

    Better do this serverside.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Jan 17, 2006
    #11
  12. JRS: In article <CWVyf.3709$>, dated
    Mon, 16 Jan 2006 17:27:05 remote, seen in news:comp.lang.javascript,
    Melvin G. Sheppers <> posted :
    >I want to display the time in a particular timezone (US Central) regardless
    >where the computer is located. I have written the script below which
    >displays the time in the Eastern, Central, Mountain, Pacific and Alaska time
    >zones in the US. It works okay if the computer is located in the Central
    >timezone but not if it is located elsewhere. I know how to get the UTC time
    >and the timezoneOffset but I can't figure out how to get from there to
    >Central time. Can anyone offer any suggestions?


    Read the newsgroup FAQ; see below.

    ><SCRIPT LANGUAGE="JavaScript">


    Deprecated form.

    >var dayName=new Array(7)
    >dayName[0]="Sun"
    >dayName[1]="Mon"
    >dayName[2]="Tue"
    >dayName[3]="Wed"
    >dayName[4]="Thu"
    >dayName[5]="Fri"
    >dayName[6]="Sat"


    var dayName = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

    is better. Note that it allows in addition for the standard numbering
    of the days of the week.


    >var theDay=now.getDay()
    >var theDisplayDay = dayName[theDay]


    >document.forms[3].displayday.value= theDisplayDay
    >document.forms[3].displayhour.value= theDisplayHour-2
    >document.forms[3].displaymin.value= theMin
    >document.forms[3].displaysec.value= theSec




    > timerID=setTimeout("countDown()",1000)


    That will not, or not always, be synchronous with the computer's
    seconds.


    Since, unlike the EU, the US does not change simultaneously (+- <0.9 s)
    between Summer and Winter time - and indeed has locations that have no
    Summer - your approach must inevitably fail for at least some locations
    and date/times. Especially if run in much of Arizona, or in Hawaii.

    You should also note that the day, date, year and the month can vary
    across the USA.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Jan 17, 2006
    #12
  13. JRS: In article <>
    , dated Mon, 16 Jan 2006 15:44:09 remote, seen in
    news:comp.lang.javascript, <>
    posted :
    >I think there is a function setTimeZoneOffset() of the Date object that
    >lets you set hours of offset from GMT (general mean time i.e. greenwich
    >england)


    Check before you post.

    (a) It does not exist, at least in ECMA, small Flanagan I, and IE 4.
    (b) If it did, it would have a z not a Z.
    (c) GMT => Greenwich Mean Time, not 'general'.

    --
    © John Stockton, Surrey, UK. ???@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Check boilerplate spelling -- error is a public sign of incompetence.
    Never fully trust an article from a poster who gives no full real name.
    Dr John Stockton, Jan 17, 2006
    #13
  14. JRS: In article <Xns974DC24F97501castleamber@130.133.1.4>, dated Tue,
    17 Jan 2006 01:06:09 remote, seen in news:comp.lang.javascript, John
    Bokma <> posted :
    >"" <> wrote:
    >
    >> How do they have fractions of an hour? Timezones are solid hour by
    >> hour last time I checked.

    >
    >IIRC, last time I visted Sri Lanka it was GMT+6.5 hrs (or 5.5).


    You should go more often; it's GMT+6 now, though India remains GMT+5:30.

    I had to change js-date5.htm#Demo drop-down from Sri Lanka to India when
    I discovered that.

    The change occurred between when my 2004 & 2004 IoP diaries were
    finalised.

    Zones +5:45 and +12:45 exist, or did so recently.

    Tonga appears to be +13:00, +14:00 if it has Summer Time (unchecked).

    <URL:http://www.merlyn.demon.co.uk/misctime.htm#Zones> refers.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
    Dr John Stockton, Jan 17, 2006
    #14
  15. Melvin G. Sheppers

    Guest

    yeah sorry I was thinkign of the getTimezoneOffset() function, how did
    you check?
    And it is general time, its the standard, although the name is
    greenwich mean time sorry. Also for (b) how do you know, are there
    capitalization rules in the EMCA standard?
    , Jan 18, 2006
    #15
  16. Melvin G. Sheppers

    John Bokma Guest

    Dr John Stockton <> wrote:

    > JRS: In article <Xns974DC24F97501castleamber@130.133.1.4>, dated Tue,
    > 17 Jan 2006 01:06:09 remote, seen in news:comp.lang.javascript, John
    > Bokma <> posted :
    >>"" <> wrote:
    >>
    >>> How do they have fractions of an hour? Timezones are solid hour by
    >>> hour last time I checked.

    >>
    >>IIRC, last time I visted Sri Lanka it was GMT+6.5 hrs (or 5.5).

    >
    > You should go more often;


    True, true. It has been almost 7 years ago. Wonderfull country, wonderfull
    people :-D.

    > Tonga appears to be +13:00, +14:00 if it has Summer Time (unchecked).


    :-D. Always ahead of us.

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
    John Bokma, Jan 18, 2006
    #16
  17. JRS: In article <>, dated Wed, 18
    Jan 2006 09:44:26 remote, seen in news:comp.lang.javascript, Jasen Betts
    <> posted :

    >> Tonga appears to be +13:00, +14:00 if it has Summer Time (unchecked).

    >
    >Tonga being tropical has days that are pretty much the same length year round
    >(thus no summer time there, no summer either for that matter)
    >OTOH the Chatham Islands currently are in summer time +13:45.


    Some places have set their time for consistency with "partner"s, rather
    than by geographical logic (examples; the far East and West of India and
    of China).

    I have heard that Tonga had Summer Time, and that Christmas Island was
    GMT+14. Some places changed for the Millennium, and may have changed
    back.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
    Dr John Stockton, Jan 18, 2006
    #17
  18. Melvin G. Sheppers

    Paul Cooper Guest

    On Wed, 18 Jan 2006 22:12:35 +0000, Dr John Stockton
    <> wrote:


    >
    >Some places have set their time for consistency with "partner"s, rather
    >than by geographical logic (examples; the far East and West of India and
    >of China).
    >
    >I have heard that Tonga had Summer Time, and that Christmas Island was
    >GMT+14. Some places changed for the Millennium, and may have changed
    >back.
    >


    And even more so in Antarctica, where the majority of occupied bases
    run on a time-zone convenient for their logistic support, not
    according tolongitude. For example, Rothera (68 degrees west) should
    be at UTC - 5; it in fact runs at UTC -4 to coincide with Port Stanley
    in the Falkland Islands. Of course, I ignore summertime adjustments
    here. Other examples run even further away from their "natural"
    timezone. Of course, the question of "what time is it at the South
    Pole" is a bit tricky! As far as I know it is Pacific Standard Time,
    but I am not absolutely certain, and of course it is simply a matter
    of convenience at the point where all timezones meet in a point!

    Paul
    Paul Cooper, Jan 20, 2006
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. engsol
    Replies:
    2
    Views:
    928
    Dan Bishop
    Jan 26, 2004
  2. Replies:
    8
    Views:
    460
    Magnus Lycka
    Aug 5, 2005
  3. Peter Hansen
    Replies:
    0
    Views:
    702
    Peter Hansen
    Feb 22, 2006
  4. Kevin Frey
    Replies:
    0
    Views:
    601
    Kevin Frey
    Nov 30, 2006
  5. flamesrock
    Replies:
    8
    Views:
    434
    Hendrik van Rooyen
    Nov 24, 2006
Loading...

Share This Page