By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,871 Members | 2,595 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,871 IT Pros & Developers. It's quick & easy.

Help Displaying Time

P: n/a
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>
Jan 16 '06 #1
Share this Question
Share on Google+
19 Replies


P: n/a
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 '06 #2

P: n/a
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>

Jan 17 '06 #3

P: n/a
How do they have fractions of an hour? Timezones are solid hour by
hour last time I checked.

Jan 17 '06 #4

P: n/a
mo********@gmail.com 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
Jan 17 '06 #5

P: n/a
Weird, never heard of that.

Jan 17 '06 #6

P: n/a
"mo********@gmail.com" <mo********@gmail.com> 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).

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jan 17 '06 #7

P: n/a
mick white <mi**@mickweb.com> wrote:
mo********@gmail.com 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


Yup, same for Sri Lanka IIRC (been there, done that)

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jan 17 '06 #8

P: n/a
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.getTimezone Offset()*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
Jan 17 '06 #9

P: n/a
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)
Jan 17 '06 #10

P: n/a
On 2006-01-16, mo********@gmail.com <mo********@gmail.com> wrote:
How do they have fractions of an hour? Timezones are solid hour by
hour last time I checked.


some are half-hour offsets, but the chatham islands are 12:45 ahead of GMT
population somewhere less than 1000 (actually 13:45 ahead because of
daylight savings)
$ date -u
Tue Jan 17 11:18:23 UTC 2006

$ TZ=/usr/share/zoneinfo/right/Pacific/Chatham date
Wed Jan 18 01:03:04 CHADT 2006

I know of no more extreme timezone than that.

And now:
It's bed time.
Goodnight.

Bye.
Jasen
Jan 17 '06 #11

P: n/a
JRS: In article <CW*****************@bignews5.bellsouth.net>, dated
Mon, 16 Jan 2006 17:27:05 remote, seen in news:comp.lang.javascript,
Melvin G. Sheppers <sh******@bellsouth.net> 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.
Jan 17 '06 #12

P: n/a
JRS: In article <Xn*************************@130.133.1.4>, dated Tue,
17 Jan 2006 01:06:09 remote, seen in news:comp.lang.javascript, John
Bokma <jo**@castleamber.com> posted :
"mo********@gmail.com" <mo********@gmail.com> 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.
Jan 17 '06 #13

P: n/a
JRS: In article <11**********************@g14g2000cwa.googlegroups .com>
, dated Mon, 16 Jan 2006 15:44:09 remote, seen in
news:comp.lang.javascript, mo********@gmail.com <mo********@gmail.com>
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.
Jan 17 '06 #14

P: n/a
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 '06 #15

P: n/a
Dr John Stockton <jr*@merlyn.demon.co.uk> wrote:
JRS: In article <Xn*************************@130.133.1.4>, dated Tue,
17 Jan 2006 01:06:09 remote, seen in news:comp.lang.javascript, John
Bokma <jo**@castleamber.com> posted :
"mo********@gmail.com" <mo********@gmail.com> 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
Jan 18 '06 #16

P: n/a
On 2006-01-17, Dr John Stockton <jr*@merlyn.demon.co.uk> wrote:
JRS: In article <Xn*************************@130.133.1.4>, dated Tue,
17 Jan 2006 01:06:09 remote, seen in news:comp.lang.javascript, John
Bokma <jo**@castleamber.com> posted :
"mo********@gmail.com" <mo********@gmail.com> 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).


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.

Bye.
Jasen
Jan 18 '06 #17

P: n/a
JRS: In article <3c*****************@clunker.homenet>, dated Wed, 18
Jan 2006 09:44:26 remote, seen in news:comp.lang.javascript, Jasen Betts
<ja***@free.net.nz> 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.
Jan 18 '06 #18

P: n/a
On Wed, 18 Jan 2006 22:12:35 +0000, Dr John Stockton
<jr*@merlyn.demon.co.uk> 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
Jan 20 '06 #19

P: n/a
VK

Paul Cooper wrote:
On Wed, 18 Jan 2006 22:12:35 +0000, Dr John Stockton
<jr*@merlyn.demon.co.uk> 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.


If one decided to collect all "interesting facts" about human calendar
then:
<http://www.worldtimezone.com/faq.html>

One fact is missing even there: Spirit and Opportunity management teams
are living (and posting reports) by Mars' calendar with "sol" (Mars'
day)

Not sure though what all of this has to do with tea and China?

As I explained in another thread, calendar (inlike milliseconds count)
is based on *regulations*, not *calculations*. Therefore any attempts
to establish some booletproof one-to-one relations between date and
physical time are futile. JavaScript Date object is good enough
*helper* for not-too-far-back / not-too-far-forward tasks. For anything
more serious there are much more serious tool like say Calendar which
is a (internally) a huge collection of different calendar regulations
and their application rules. For even more serious (state level) task
there are special organisations like International Earth Rotation
Service <http://www.iers.org/>

Jan 20 '06 #20

This discussion thread is closed

Replies have been disabled for this discussion.