Changing Picture Based On Time

Hi,
I am new to Javascript and am really look for some help. I am trying to make a webpage that will change a picture based on the time. I would like one picture (image2) to display between 8:23 and 2:43 Monday-Friday and another(image1) to display the rest of the time. When I open the file the web browser only displays a blank screen. I am probably just making some kind of beginner mistake so I would really appreciate it if someone could help me out.

I have the two images in the same directory as the html file. Here is the code:


<html>
<body>
<script type="text/javascript">
	var day = new Date();
	var today = day.getDay();
	var hour = day.getHours();
	var minutes = day.getMinutes();

	if (today>0 && today<6)
		{if (hour = 8)
			{if (minutes > 23)
				{document.write("<img src="image2.jpg">")}
			else
				{document.write("<img src="image1.jpg">")}
			}
		else if (hour>8 && hour<14)
			{document.write("<img src="image2.jpg">")}
		else if (hour = 2)
			{if (minutes < 43)
				{document.write("<img src="image2.jpg">")}
			else
				{document.write("<img src="image1.jpg">")}
			}
		else
			{document.write("<img src="image1.jpg>")}
		</script>
</body>
</html>

If you can point anything out that would be great.

Thanks!

The if statements appear to be missing their closing parenthesis.

You can also create a date for 8:23 today


var startTime = new Date();
startTime.setHours(8);
startTime.setMinutes(23);
startTime.setSeconds(0);

And then likewise for the end time.

Then, if you rename the day variable to now, you can perform checks such as if startTime > now && endTime < now

Thanks for your reply. I believe i made the changes that you suggested.


<<html>
<body>
<script type="text/javascript">
	var now = new Date();
	var today = Date.getDay;
	var startTime = new Date();
	startTime.setHours(8);
	startTime.setMinutes(23);
	startTime.setSeconds(0);
	var endTime = new Date();
	endTime.setHours(14);
	endTime.setMinutes(43);
	endTime.setSeconds(0);

	if (today > 0 && today < 6)
		{
		if (startTime > now && endTime < now)
			{document.write("<img src="image2.jpg">");}
		else
			{document.write("<img src="image1.jpg">");}
		}
	else
		{document.write("<img src="image1.jpg">");}
</script>
</body>
</html>

Your idea of the start, end time definitely made the code shorter. Unfortunately, the browser still shows a white screen. Any other suggestions?

Thanks again.

The double quotes used to mark the strings will be causing trouble too.

Instead of
“<img src=“image1.jpg”>”

use single quotes to mark up the test strings instead
‘<img src=“image1.jpg”>’

It ill also help if you use your web browser’s features to find out what the code troubles are.

For example, with Firefox you will find in the Tools menu an Error Console that lets you know what the errors are.

YES!!! My browser now displays an image!
However, for some reason the code doesn’t agree with: today > 0 && today < 6.
Therefore it won’t go into the next if statement.
Thanks though for helping me at least get an image!
Here is the current code.


<html>
<body>
<script type="text/javascript">
	var now = new Date();
	var today = now.getDay;
	var startTime = new Date();
	startTime.setHours(8);
	startTime.setMinutes(23);
	startTime.setSeconds(0);
	var endTime = new Date();
	endTime.setHours(14);
	endTime.setMinutes(43);
	endTime.setSeconds(0);

	if (today > 0 && today < 6)
		{
		if (startTime > now && endTime < now)
			{document.write('<img src="image2.jpg">');}
		else
			{document.write('<img src="image1.jpg">');}
		}
	else
		{document.write('<img src="image1.jpg">');}
</script>
</body>
</html>

getDay is a method, not a parameter, so stick some parenthesis on there and you should be right.

Thank you so much! It worked!
Here is the final code for anyone that wants it.


<html>
<body>
<script type="text/javascript">
	var now = new Date();
	var today = now.getDay();
	var startTime = new Date();
	startTime.setHours(08);
	startTime.setMinutes(23);
	startTime.setSeconds(00);
	var endTime = new Date();
	endTime.setHours(14);
	endTime.setMinutes(43);
	endTime.setSeconds(00);

	if (today > 0 && today < 6)
		{
		if (startTime < now && endTime > now)
			{document.write('<img src="image2.jpg">');}
		else
			{document.write('<img src="image1.jpg">');}
		}
	else
		{document.write('<img src="image1.jpg">');}
</script>
</body>
</html>

Thanks again!