Click to See Complete Forum and Search --> : Adding Day of the Year to time of day script


Rusty073
10-09-2005, 02:01 AM
Gday all

Can someone please help me. I need to add a day/month/year so this script will only come on at a certain day of the current year. If someone can just rewrite the code with an example day it would be much appreciated

thanks in advance
Shane

<script language = 'JavaScript'>
var time = new Date();
var hours = time.getUTCHours();
var minutes = time.getUTCMinutes();
var lim = (hours*60)+minutes;

if(lim>=(23*60+20) && lim<=(23*60+39)){
document.write("<IMG SRC='http://www.westairairlines.com/images/Buttons/Anims/Boardingsoonanim.gif'>");
}
else if(lim>=(23*60+40) && lim<=(23*60+59)){
document.write("<IMG SRC='http://www.westairairlines.com/images/Buttons/Anims/Boardingnowanim.gif'>");
}
else if(lim>=(0*60+0) && lim<=(0*60+19)){
document.write("<IMG SRC='http://www.westairairlines.com/images/Buttons/Anims/departinganim.gif'>");
}
else if(lim>=(0*60+20) && lim<=(1*60+45)){
document.write("<IMG SRC='http://www.westairairlines.com/images/Buttons/Anims/inflightanim.gif'>");
}
else if(lim>=(1*60+46) && lim<=(2*60+5)){
document.write("<IMG SRC='http://www.westairairlines.com/images/Buttons/Anims/landedanim.gif'>");
}
else
document.write("<IMG SRC='http://www.westairairlines.com/images/Buttons/Anims/FlightClosed.jpg'>");
</script>

pccode
10-09-2005, 04:46 AM
<html>
<head>
<script>
var months = new Array(13);
months[1] = "January";
months[2] = "February";
months[3] = "March";
months[4] = "April";
months[5] = "May";
months[6] = "June";
months[7] = "July";
months[8] = "August";
months[9] = "September";
months[10] = "October";
months[11] = "November";
months[12] = "December";
var time = new Date();
var lmonth = months[time.getMonth() + 1];
var date = time.getDate();
var year = time.getYear();
if (year < 2000) { year = year + 1900; }

//create an array of your chosen dates
var datearray = new Array(5);
datearray[0] = "October 5";
datearray[1] = "October 10";
datearray[2] = "October 15";
datearray[3] = "October 20";
datearray[4] = "October 25";

if (lmonth + " " + date == datearray[0]) { document.write("<IMG SRC='http://www.westairairlines.com/images/Buttons/Anims/Boardingsoonanim.gif'>"); }
else if (lmonth + " " + date == datearray[1]) { document.write("<IMG SRC='http://www.westairairlines.com/images/Buttons/Anims/Boardingnowanim.gif'>"); }
else if (lmonth + " " + date == datearray[2]) { document.write("<IMG SRC='http://www.westairairlines.com/images/Buttons/Anims/departinganim.gif'>"); }
else if (lmonth + " " + date == datearray[3]) { document.write("<IMG SRC='http://www.westairairlines.com/images/Buttons/Anims/inflightanim.gif'>"); }
else if (lmonth + " " + date == datearray[4]) { document.write("<IMG SRC='http://www.westairairlines.com/images/Buttons/Anims/landedanim.gif'>"); }
else { document.write("<IMG SRC='http://www.westairairlines.com/images/Buttons/Anims/FlightClosed.jpg'>"); }
</script>
</head>
<body>
</body>
</html>

Rusty073
10-09-2005, 05:26 AM
Thank You PCCODE i will endeavour to use this scrip as it helps me out tremdoulsy thank you for your input as it helps make my site which is a virtual aviation site more dynamic thanks again

Rusty073
10-09-2005, 05:30 AM
Just one more thing. How would i put both the dates and time in to make it read at a certain time on a certian date as the original showed UTC time and now i need the date to be added to the script so that on x date at X time this happens

Thanks

pccode
10-09-2005, 06:56 AM
<html>
<head>
<script>

var time = new Date();
var hours = time.getUTCHours();
var minutes = time.getUTCMinutes();
var clock = hours + ":" + minutes;

var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
var time = new Date();
var lmonth = months[time.getMonth()];
var date = time.getDate();
var year = time.getYear();
if (year < 2000) { year = year + 1900; }

//create an array of your chosen dates
var datearray = new Array(5);
datearray[0] = "October 9";
datearray[1] = "October 10";
datearray[2] = "October 15";
datearray[3] = "October 20";
datearray[4] = "October 25";

//create an array of your chosen times
var clockarray = new Array(5);
clockarray[0] = "11:52";
clockarray[1] = "3:30";
clockarray[2] = "5:15";
clockarray[3] = "6:51";
clockarray[4] = "9:37";

if (lmonth + " " + date == datearray[0] && clock == clockarray[0]) { document.write("<IMG SRC='http://www.westairairlines.com/images/Buttons/Anims/Boardingsoonanim.gif'>"); }
else if (lmonth + " " + date == datearray[1] && clock == clockarray[1]) { document.write("<IMG SRC='http://www.westairairlines.com/images/Buttons/Anims/Boardingnowanim.gif'>"); }
else if (lmonth + " " + date == datearray[2] && clock == clockarray[2]) { document.write("<IMG SRC='http://www.westairairlines.com/images/Buttons/Anims/departinganim.gif'>"); }
else if (lmonth + " " + date == datearray[3] && clock == clockarray[3]) { document.write("<IMG SRC='http://www.westairairlines.com/images/Buttons/Anims/inflightanim.gif'>"); }
else if (lmonth + " " + date == datearray[4] && clock == clockarray[4]) { document.write("<IMG SRC='http://www.westairairlines.com/images/Buttons/Anims/landedanim.gif'>"); }
else { document.write("<IMG SRC='http://www.westairairlines.com/images/Buttons/Anims/FlightClosed.jpg'>"); }

//the code below will display the current date and UTC time
//use the code below to test the script
//remove it when you are ready to insert the script into your page
alert(lmonth + " " + date + "\n" + clock);
</script>
</head>
<body>
</body>
</html>


Did you want to convert the UTC time to a different time zone like eastern or central?

The problem with getting the time with javascript is that it determines the time by each user's computer clock. If their computer clock is wrong, then it will probably affect your script. Unfortunately with javascript there's no way to get around this. If you have support for php or ssi I would suggest using one of those options to retrieve the time instead of javascript.

EDIT: I've changed the script to reflect Felgall's suggestion.

Rusty073
10-09-2005, 07:14 AM
Gday pccode

yes my server handles both PHP and ssi but i am relevantly new to webpage design so i will just put a note on there that the light script will only work true if pc clock is the right time

Shane

felgall
10-09-2005, 03:39 PM
if you use:

var months = ['January','February','March','April','May','June','July','August','September','October','November',' December'];

then you can forget about adding 1 to the month field as it will reference the correct month from the array directly as intended rather than having an extra blank entry on the front of the array.