Computing.Net > Forums > Web Development > Add BST & GMT to this javascript?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Add BST & GMT to this javascript?

Reply to Message Icon

Name: Stephen Allden
Date: October 7, 2006 at 18:09:56 Pacific
OS: Microsoft Windows XP Prof
CPU/Ram: 534 MHz/192 MB
Product: Packard Bell (Nec)
Comment:

Hiya,

I have a page on my website with a JavaScript code on which basically displays different images at different times of the day but the problem is the code looks for the visitors system time and I don't want it to do that, does anyone know how I add a BST (British summer time) and GMT function to the following code...

http://www.stephenallden.co.uk/javascript_code.htm

Cheers!!!



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: October 7, 2006 at 22:06:06 Pacific
Reply:

Add the items in blue to make the visitor's time GMT time. (make any adjustment you need to make it BST):

day=new Date();
offset = time.getTimezoneOffset();
h = day.getHours()*60;
m = day.getMinutes();
time = h + m + offset;

Michael J


0

Response Number 2
Name: Michael J (by mjdamato)
Date: October 7, 2006 at 22:28:28 Pacific
Reply:

I just realized, because you will be adjusting the user's time it will be possible to get invalid times (i.e. less than 0 or greater than 1440). So, you will need to adjust for this. Also, your last "if" statement is not needed. If none of the other conditions were met, you just need the else statement.

I also made some changes to make the code a little more compact. Here is the entire code with the modifications needed:

==========BEGIN CODE==============

day = new Date();
hours = day.getHours()*60;
minutes = day.getMinutes();
offset = day.getTimezoneOffset();
time = hours + minutes + offset;

if (time < 0) {time = time + 1440}
if (time > 1440) {time = time - 1440}

if (time<300) {img = '1';} // 12:00 am
else if (time<600) {img = '2';} // 5:00 am
else if (time<720) {img = '3';} // 10:00 am
else if (time<840) {img = '4';} // 12:00 pm
else if (time<960) {img = '5';} // 2:00 pm
else if (time<1020) {img = '6';} // 4:00 pm
else if (time<1050) {img = '7';} // 5:00 pm
else if (time<1140) {img = '8';} // 5:30 pm
else if (time<1320) {img = '9';} // 7:00 pm
else {img = '10';} // 10:00 pm

document.write('[img border="1" src="'+img+'.gif" width="64" height="64"]');

==========END CODE==============

Note: replace the [] in the last line with <>. Changed for posting purposes.


Michael J


0

Response Number 3
Name: Stephen Allden
Date: October 8, 2006 at 10:34:42 Pacific
Reply:

Hiya what part of the code do i add the BST, do i remove the getTimezoneOffset from the line offset = day.getTimezoneOffset();

Cheers!!!


0

Response Number 4
Name: Michael J (by mjdamato)
Date: October 8, 2006 at 18:14:49 Pacific
Reply:

"Hiya what part of the code do i add the BST, do i remove the getTimezoneOffset from the line offset = day.getTimezoneOffset();"

No, the getTimezoneOffset function gets the offset from the user's time (based upon their computer settings) from GMT. So the line time = hours + minutes + offset; sets the variable "time" to the current GMT based upon the time set for the user's computer. You would need to adjust that to get BST.

For example, if BST is 2 hours ahead of GMT then you would change the line accordingly:

time = hours + minutes + offset + 120;

A couple things to remember. This is dependant on the fact that the user's timezone is set correctly on their computer and that their time is correct. Also, there is no way to account for daylight savings time is the user is using that.

To really do this correctly it would be better to use some server side scripting.

Michael J


0

Response Number 5
Name: Stephen Allden
Date: October 11, 2006 at 18:09:42 Pacific
Reply:

Hiya Michael J,

Thanks for the above code mate work brilliant just what i needed cheers. How do i change the code so that instead of images i can have text displaying with the capability of changing font name, size, colour, etc.

Cheers!!!


0

Related Posts

See More



Response Number 6
Name: Michael J (by mjdamato)
Date: October 11, 2006 at 22:26:27 Pacific
Reply:

Change the variable text of the if conditions to what you want each of the different displays to be. Then change the code within the document.write function to be whatever format you want the variables to be displayed.

document.write('[span style="font-size:12pt;font-weight:bold;"]VariableText[/span]');

Michael J


0

Response Number 7
Name: Stephen Allden
Date: October 12, 2006 at 06:54:15 Pacific
Reply:

Hiya Michael J,

I've made the necessary changes as you suggested but it don't seem to work, can you have a look at the code below and tell me what I’ve done wrong,

Cheers!!!

------------------- CODE --------------------

day = new Date();
hours = day.getHours()*60;
minutes = day.getMinutes();
offset = day.getTimezoneOffset();
time = hours + minutes + offset + 60;

if (time < 0) {time = time + 1440}
if (time > 1440) {time = time - 1440}

if (time<300) {span = '12:00AM - 5:00AM';} // 12:00 am
else if (time<600) {span = '5:00AM - 10:00AM';} // 5:00 am
else if (time<720) {span = '10:00AM - 12:00PM';} // 10:00 am
else if (time<840) {span = '12:00PM - 2:00PM';} // 12:00 pm
else if (time<1140) {span = '2:00PM - 7:00PM';} // 2:00 pm
else if (time<1230) {span = '7:00PM - 8:30PM';} // 7:00 pm
else if (time<1320) {span = '8:30PM - 10:00PM';} // 8:30 pm
else {span = '10:00PM - 12:00AM';} // 10:00 pm

document.write('<span style="font-size:12pt;font-weight:bold;"]VariableText</span>');

----------------


0

Response Number 8
Name: Michael J (by mjdamato)
Date: October 12, 2006 at 07:15:44 Pacific
Reply:

Sure, but please when posting a problem, try to be descriptive. Just saying "it don't seem to work" tells me nothing about what the problem might be.

It looks like you just tried to do it in a rush. I'm sure if you were to look over your code you would see the problem. In the IF statements you are creating a variable called "span". But in the document.write command there are two problems.

1) You are not using the "span" variable you created before.

2) You left one of the square brackets in. It needs to be changed to a ">".

This should work:
document.write('<span style="font-size:12pt;font-weight:bold;">'+span+'</span>');

Michael J


0

Response Number 9
Name: Stephen Allden
Date: October 12, 2006 at 07:52:15 Pacific
Reply:

Sorry about that Michael J!!!

How bout making the code to open another page (new.htm) in a new window (tarfet="_blank")

Cheers!!!

Really appreciate your help, i'm useless at this stuff :-)!!!


0

Response Number 10
Name: Michael J (by mjdamato)
Date: October 12, 2006 at 20:37:15 Pacific
Reply:

Open WHAT in a new page?

Just change the document.write command to "print" whatever youwant to the page. Just take a look at how the content is formatted and you can make it whatever you want.

In the below example anything inside the parens () is printed. You need to encolse "text" within single quotes and you append pieces of text and variables with the add symbol (+). I have color coded the text as follows.

Javascript function - black
Text - red
Variables - blue

document.write('<span style="font-size:12pt;font-weight:bold;">'+span+'</span>');

I believe that if you give a man a fish he will eat for a day. Teach him to fish and you feed him for life. So, with that I leave you to at least ATTEMPT to make the changes yourself. If you have trouble post back with the code you have and the problems you are experiencing. Good luck.


Michael J


0

Response Number 11
Name: Stephen Allden
Date: October 13, 2006 at 13:54:37 Pacific
Reply:

Hiya Michael, no luck mate, had a crack at it but it didn't work, i'll get there one day!!!

------------------- CODE --------------------

day = new Date();
hours = day.getHours()*60;
minutes = day.getMinutes();
offset = day.getTimezoneOffset();
time = hours + minutes + offset + 60;

if (time < 0) {time = time + 1440}
if (time > 1440) {time = time - 1440}

if (time<300) {a href= '1';} // 12:00 am
else if (time<600) {a href= '2';} // 5:00 am
else if (time<720) {a href= '3';} // 10:00 am
else if (time<840) {a href= '4';} // 12:00 pm
else if (time<1140) {a href= '5';} // 2:00 pm
else if (time<1230) {a href= '6';} // 7:00 pm
else if (time<1320) {a href= '7';} // 8:30 pm
else {a href= '8';} // 10:00 pm

document.write('');

----------------

Thanks for everything, really appreciate your help!


0

Response Number 12
Name: Stephen Allden
Date: October 13, 2006 at 13:56:09 Pacific
Reply:

Ooops!!! forgot the document.write bit, sorry!!!

document.write('[a href target="_frame758500" src="'+a href+'.htm"]');


0

Response Number 13
Name: Michael J (by mjdamato)
Date: October 13, 2006 at 23:15:00 Pacific
Reply:

Variables can't have a space in them. Change "a href' to "ahref" or just "href".

When in doubt keep it simple.

Michael J


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Add BST & GMT to this javascript?

Add minutes to this javascript! www.computing.net/answers/webdevel/add-minutes-to-this-javascript/2221.html

please critique my javascript www.computing.net/answers/webdevel/please-critique-my-javascript/2982.html

Adding a print page www.computing.net/answers/webdevel/adding-a-print-page/3337.html