PDA

View Full Version : Time script


djfenom
11-07-2002, 08:39 AM
I'm looking for a piece of code that will allow me change a page depending on what time it is.

For example I have to put a press release live at work, but it can't go live before 17.30, and rather than staying at work early, I wondered if there was a way to do this in Javascript.

So, if it's >17.30 on a given date go to news2.html otherwise go to news.html.

Thanks.

piglet
11-07-2002, 11:45 AM
Hi,

The problem you have there is that Javascript is a Client side technology - it runs on your users computer.

Given this you're relient on your user's computers clock. Not only would it rely on your user having the right time on their machine - but they could be in any timezone in the world....so their time could be up to 24 hours different to yours

So the answer to your question is - yes - you can do this, but in practice it's not going to work as you want.

You're better off doing this with a server side script where you decide what the user gets from the time on the server.

djfenom
11-07-2002, 11:50 AM
Thanks for that piglet. The site that I am building is really only for the UK market, so I can probably get away with doing it by the user's computer clock.

If you have any code for this I would appreciate that.

Thanks.

piglet
11-07-2002, 12:13 PM
Sure - no problem...I'll just knock up:

<SCRIPT LANGUAGE="JavaScript">
<!--
var x = new Date()
var minutes = x.getMinutes();
var hours = x.getHours();

//remove this alert!
alert("Hours:" + hours +", Minutes:"+minutes)

if ((hours>17) || (hours==17 && minutes>30))
location.href = "news2.html";
else
location.href = "news.html";
//-->
</SCRIPT>

Simrey
11-07-2002, 06:04 PM
It's comforting to see that I'm not the only one who gets caught out by this ...

I think that line

if ((hours>17) || (hours=17 && minutes>30))

should be

if ((hours>17) || (hours==17 && minutes>30))

piglet
11-08-2002, 08:30 AM
LOL - that's what you get for just typing it in and not checking.

Never mind - you got the idea!

RMLaundon
11-08-2002, 02:08 PM
I have decided to take my main website offline for 2 minutes on the 11th November (obvious reasons) and was searching for a code for days until I found this one.

The problem is that I will be away at the time and want it to work without any interaction from myself.

Looks easy enough but I just want make sure I have things straight. Can I use this to change to a new index page at 11:00 on 11th november 2002.

To go back, I presume I require the same code to be present on the "new" page but set at 11:02?


Wouldn't you also require a date variable to be present as well?

piglet
11-08-2002, 02:23 PM
Hi,

Yes - you can flip over to a new page on the date:


<HTML><HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
var now = new Date()
var when = new Date("Nov 11 2002")

if (now > when)
location.href = "newpage.html";
else
location.href = "oldpage.html";
//-->
</SCRIPT>
</HEAD></HTML>


If your index page is the first one your visitor sees you'll need to rename it as per the name (oldpage.html) in the script above, and make the script above the index page.



Probably better to use:

if (now > when)
location.replace("newpage.html");
else
location.replace("oldpage.html");

RMLaundon
11-08-2002, 02:26 PM
Thanks for the quick reply.

These codes will also come in useful for other projects I have.

djfenom
11-11-2002, 09:30 AM
Thank you everybody, you've been a great help.