Name: Stephen Allden Date: October 7, 2006 at 18:09:56 Pacific Subject: Add BST & GMT to this javascript? OS: Microsoft Windows XP Prof CPU/Ram: 534 MHz/192 MB Model/Manufacturer: 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...
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
"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.
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.
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.
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>');
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
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.
The information on Computing.Net is the opinions of its users. Such
opinions may not be accurate and they are to be used at your own risk.
Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE