Help Displaying Time

M

Melvin G. Sheppers

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>
 
M

mouseit101

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)
 
M

mick white

Melvin said:
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>
 
M

mouseit101

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

Lee

(e-mail address removed) 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.
 
M

mick white

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
 
J

John Bokma

mick white said:
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)
 
R

RobG

Melvin said:
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:

<SCRIPT LANGUAGE="JavaScript">

The language attribute is deprecated, type is required:

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>


[...]
 
E

Evertjan.

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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Mon, 16 Jan 2006 17:27:05 remote, seen in
Melvin G. Sheppers said:
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.
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Mon, 16 Jan 2006 15:44:09 remote, seen in
news:comp.lang.javascript said:
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'.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue,
17 Jan 2006 01:06:09 remote, seen in John
Bokma said:
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.
 
M

mouseit101

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?
 
J

John Bokma

Dr John Stockton said:
JRS: In article <[email protected]>, dated Tue,
17 Jan 2006 01:06:09 remote, seen in John


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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Wed, 18
Jan 2006 09:44:26 remote, seen in Jasen Betts
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.
 
P

Paul Cooper

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top