Forum Moderators: open

Message Too Old, No Replies

uk Office Hours in User Time Zone

         

walker

9:07 pm on Feb 23, 2004 (gmt 0)

10+ Year Member



Hi

Does anyone have a good script for displaying UK office hours in the user's local time zone.

FOR EXAMPLE:

A UK Resident enters the page it sees - Office Hours 9am - 5pm

A US Resident enters the page and sees
Office Hours 5pm - 1am

Etc. Etc.

I would really apprecaite it if anyone can help.

Kind Regards

Walker

DrDoc

9:59 pm on Feb 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For the challenge of it, I wrote a quick script that does what you want... First, let's have some code:
<script type="text/javascript">
xx_timeopen = 9;
xx_timeclose = 17;
xx_timezoneoffset = 0;
ampm = 1;

timeObject = new Date();
localtimeopen = new Date(timeObject.setUTCHours(xx_timeopen + (xx_timezoneoffset * -1)));
localtimeclose = new Date(timeObject.setUTCHours(xx_timeclose + (xx_timezoneoffset * -1)));

if(ampm) {
document.write("Open between " + (localtimeopen.getHours()==12 ¦¦ localtimeopen.getHours()==0?"12" + (localtimeopen.getHours()==12?"pm":"am"):(localtimeopen.getHours()>12?localtimeopen.getHours()-12 + "pm":localtimeopen.getHours() + "am")) + " and " + (localtimeclose.getHours()==12 ¦¦ localtimeclose.getHours()==0?"12" + (localtimeclose.getHours()==12?"pm":"am"):(localtimeclose.getHours()>12?localtimeclose.getHours()-12 + "pm":localtimeclose.getHours() + "am")));
}
else {
document.write("Open between " + localtimeopen.getHours() + " and " + localtimeclose.getHours());
}
</script>

There are only four variables you need to worry about:

xx_timeopen

xx_timeclose

xx_timezoneoffset

ampm

xx_timeopen
is the local time whenever you open (in this case 9)
xx_timeclose
is the local time whenever you close (in this case 17)
xx_timezoneoffset
is the local time zone offset from UTC (which is, of course, 0 for you)
Finally,
ampm
lets you decide whether you want a 12 hour format with AM and PM in the time (ampm=1) or whether you want to display the times in 24 hour format (ampm=0)

This script works great... except for two issues:
• It can only handle even hours for the time open/close variables (specifying 8:30 is a no-go ;))
• If the xx_timezoneoffset would be some odd hours (such as +3:30) it won't work either

the "xx" part is to help you customize it for readability purposes... For example, you could call the variables

uk_timeopen
,
uk_timeclose
, and
uk_timezoneoffset

[edited by: tedster at 5:52 pm (utc) on Feb. 25, 2004]

DrDoc

7:52 am on Feb 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



JavaScript has a getTimezoneOffset function too... but it is really buggy. Some browsers don't support it at all. Others don't return the right value (IE is one hour off). So, even though it would be much easier to use getTimezoneOffset, it would not be nearly as reliable.

I worked out a version that works with minutes and stuff, but I will have to post it tomorrow when I'm back at that computer. :)

I apologize for the side scrolling in the above message... I was going to edit the post to make the code line break (and also remove the smilies), but our ISP at work went down :( And once I got home, the time limit for me to edit the post had passed

walker

10:34 am on Feb 24, 2004 (gmt 0)

10+ Year Member



WOW!

That was realy kind of you, however I tried the code and it says invalid charectar. any ideas?

Thank you

Rod

dcrombie

12:10 pm on Feb 24, 2004 (gmt 0)



Try retyping the ¦ characters. This forum mangles them for reasons unknown.

Longhaired Genius

12:33 pm on Feb 24, 2004 (gmt 0)

10+ Year Member



dcrombie's advice is a little more cryptic that he meant it to be.

Replace the ¦ characters with the "pipe" character, above the backslash at the far left on your keyboard. This forum uses "pipe" to delimit the data fields in the "flat files" so all instances of "pipe" in messages are changed to ¦.

walker

1:39 pm on Feb 24, 2004 (gmt 0)

10+ Year Member



BRILLIANT!

THANKS EVERYONE

Kind Regards

Walker

DrDoc

3:55 pm on Feb 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Same script, but now it also handles minutes...

<script type="text/javascript">
xx_houropen = 8;
xx_minuteopen = 3;
xx_hourclose = 17;
xx_minuteclose = 22;
xx_timezoneoffset = -6;
xx_minuteoffset = -45;
ampm = 1;

timeObject = new Date();
xx_timeopen = new Date(timeObject.setUTCHours(xx_houropen + (xx_timezoneoffset * -1)));
xx_timeopen = xx_timeopen.setUTCMinutes(xx_minuteopen + (xx_minuteoffset * -1));
localtimeopen = new Date(xx_timeopen);
xx_timeclose = new Date(timeObject.setUTCHours(xx_hourclose + (xx_timezoneoffset * -1)));
xx_timeclose = xx_timeclose.setUTCMinutes(xx_minuteclose + (xx_minuteoffset * -1));
localtimeclose = new Date(xx_timeclose);

if(ampm) {
document.write("Open between " + (localtimeopen.getHours()==12 ¦¦ localtimeopen.getHours()==0?"12" + (localtimeopen.getHours()==12?(localtimeopen.getMinutes()>0?":" + localtimeopen.getMinutes():"") + "pm":(localtimeopen.getMinutes()>0?":" + localtimeopen.getMinutes():"") + "am"):(localtimeopen.getHours()>12?localtimeopen.getHours()-12 + (localtimeopen.getMinutes()>0?":" + localtimeopen.getMinutes():"") + "pm":localtimeopen.getHours() + (localtimeopen.getMinutes()>0?":" + localtimeopen.getMinutes():"") + "am")) + " and " + (localtimeclose.getHours()==12 ¦¦ localtimeclose.getHours()==0?"12" + (localtimeclose.getHours()==12?(localtimeclose.getMinutes()>0?":" + localtimeclose.getMinutes():"") + "pm":(localtimeclose.getMinutes()>0?":" + localtimeclose.getMinutes():"") + "am"):(localtimeclose.getHours()>12?localtimeclose.getHours()-12 + (localtimeclose.getMinutes()>0?":" + localtimeclose.getMinutes():"") + "pm":localtimeclose.getHours() + (localtimeclose.getMinutes()>0?":" + localtimeclose.getMinutes():"") + "am")));
}
else {
document.write("Open between " + localtimeopen.getHours() + (localtimeopen.getMinutes()>0?":" + localtimeopen.getMinutes():"") + " and " + localtimeclose.getHours() + (localtimeclose.getMinutes()>0?":" + localtimeclose.getMinutes():""));
}
</script>

[edited by: tedster at 5:54 pm (utc) on Feb. 25, 2004]