PDA

View Full Version : JavaScript Date()



Tux.Ice
July 2nd, 2008, 09:14 PM
how can i get a picture on a website to change depending on the time of day?

from 6am-10am == mornlogo.png
from 10am-3pm == daylogo.png
from 3pm-5pm == setlogo.png
from 5pm-6am == nightlogo.png

could i just use an array with a Date() function and then do document.images or document.write?

LaRoza
July 2nd, 2008, 09:24 PM
how can i get a picture on a website to change depending on the time of day?

from 6am-10am == mornlogo.png
from 10am-3pm == daylogo.png
from 3pm-5pm == setlogo.png
from 5pm-6am == nightlogo.png

could i just use an array with a Date() function and then do document.images or document.write?

It would be easy...

Just use the DOM to change the image src attribute (have a default one coded in) according to the date.

Tux.Ice
July 2nd, 2008, 10:16 PM
im a pretty big n00ber at JS, how would i put a DOM into this, ii was actually thining of using an if statement.

LaRoza
July 2nd, 2008, 10:25 PM
im a pretty big n00ber at JS, how would i put a DOM into this, ii was actually thining of using an if statement.

I hope you were thinking about it...

First make the <img /> element, with a default image and give it an id. For this, I am giving it an id="time".



window.onload = function()
{
var imgElement = document.getElementById("time");
var d = new Date().getHours() + 1;
if (imgElement)
{
if (d >= 6 && d <= 10)
{
imgElement.setAttribute("src","mornlogo.png")
}
//etc
}
}