Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java script programming assistance

Status
Not open for further replies.

sromine

Technical User
Apr 21, 2006
38
0
0
US
Hello,

I am hoping someone with javascript programming experience can help me modify the below script. I think it is easy for someone who knows what they are doing. I work for a library and the below script works like a charm to show which days we are open. But....it would be great if it was slightly modified so that it included a check for holidays and then displayed "Holiday Closed" or words to that effect. I do not know javascript, but I think it is a simple if, then, else...we have multiple holidays so if somebody could just add the code for it to show "Holiday Closed" on christmas and one other date, i can modify from there for all of our dates. Thanks for whoever can give me a hand!

<script language="javascript">
var mydate=new Date();
var year=mydate.getYear();
var day=mydate.getDay();
var month=mydate.getMonth();
var daym=mydate.getDate();
var dayarray=new Array("sunday", "monday","tuesday","wednesday","thursday","friday","saturday");
var montharray=new Array("january","february","march","april","may","june","july","august","september","october","november","december");
var hourarray=new Array("<font color='#C60000'>Closed Today</font>", "open today from <font color='#C60000'>12pm&nbsp;to&nbsp;8pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;8pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;6pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;8pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;4pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;4pm</font>");
document.write("<font face='Arial' size=-2>&nbsp;&nbsp;&nbsp;" +hourarray[day]+"<br>&nbsp;&nbsp;&nbsp;<font color='#000000'>"+dayarray[day]+", "+montharray[month]+" "+daym+", "+year+"<br><font color='000000' face='Arial'>");
</script>
 
You can do it like this with modifications in colour.
[tt]
<script language="javascript">
var mydate=new Date();
var year=mydate.get[blue]Full[/blue]Year();
var day=mydate.getDay();
var month=mydate.getMonth();
var daym=mydate.getDate();
var dayarray=new Array("sunday", "monday","tuesday","wednesday","thursday","friday","saturday");
var montharray=new Array("january","february","march","april","may","june","july","august","september", "october","november","december");

[blue]var holidayarray=new Array("december 25","may 1","may 20");
var rx=new RegExp(montharray[month]+" "+daym,"i");
var ex_day=(rx.test(montharray[month]+" "+daym))?0:day;
[/blue]
var hourarray=new Array("<font color='#C60000'>Closed Today</font>", "open today from <font color='#C60000'>12pm&nbsp;to&nbsp;8pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;8pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;6pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;8pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;4pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;4pm</font>");
document.write("<font face='Arial' size=-2>&nbsp;&nbsp;&nbsp;" +hourarray[[blue]ex_[/blue]day]+"<br>&nbsp;&nbsp;&nbsp;<font color='#000000'>"+dayarray[day]+", "+montharray[month]+" "+daym+", "+year+"<br><font color='000000' face='Arial'>");
</script>
[/tt]
It uses the same hourarray[0] sunday's close for the holiday close as well. If not, you can append another hourarray[7] with specific highlight of the day being a holiday. Then the line has to be changed to having ex_day=7.
[tt]
var ex_day=(rx.test(montharray[month]+" "+daym))?[blue]7[/blue]:day;[/tt]

 
Amendment

Upon re-reading what I posted, I find that a crucial line in the idea but not made its way in to post. Here is a retake with highlight of difference.
[tt]
<script language="javascript">
var mydate=new Date();
var year=mydate.getFullYear();
var day=mydate.getDay();
var month=mydate.getMonth();
var daym=mydate.getDate();
var dayarray=new Array("sunday", "monday","tuesday","wednesday","thursday","friday","saturday");
var montharray=new Array("january","february","march","april","may","june","july","august","september", "october","november","december");

var holidayarray=new Array("december 25","may 1","may 20");
var rx=new RegExp(montharray[month]+" "+daym,"i");
[red]var ex_day=(rx.test(holidayarray.join(";")))?0:day;[/red]

var hourarray=new Array("<font color='#C60000'>Closed Today</font>", "open today from <font color='#C60000'>12pm&nbsp;to&nbsp;8pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;8pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;6pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;8pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;4pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;4pm</font>");
document.write("<font face='Arial' size=-2>&nbsp;&nbsp;&nbsp;" +hourarray[ex_day]+"<br>&nbsp;&nbsp;&nbsp;<font color='#000000'>"+dayarray[day]+", "+montharray[month]+" "+daym+", "+year+"<br><font color='000000' face='Arial'>");
</script>
[/tt]
 
Thanks, worked like a charm. Following your instructions, I modified to make it the following:

<script language="javascript">
var mydate=new Date();
var year=mydate.getFullYear();
var day=mydate.getDay();
var month=mydate.getMonth();
var daym=mydate.getDate();
var dayarray=new Array("sunday", "monday","tuesday","wednesday","thursday","friday","saturday");
var montharray=new Array("january","february","march","april","may","june","july","august","september", "october","november","december");

var holidayarray=new Array("May 29","July 4","September 4");
var rx=new RegExp(montharray[month]+" "+daym,"i");
var ex_day=(rx.test(holidayarray.join(";")))?7:day;

var hourarray=new Array("<font color='#C60000'>Closed Today</font>", "open today from <font color='#C60000'>12pm&nbsp;to&nbsp;8pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;8pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;6pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;8pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;4pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;4pm</font>","<font color='#C60000'>Holiday - Closed Today</font>");
document.write("<font face='Arial' size=-2>&nbsp;&nbsp;&nbsp;" +hourarray[ex_day]+"<br>&nbsp;&nbsp;&nbsp;<font color='#000000'>"+dayarray[day]+", "+montharray[month]+" "+daym+", "+year+"<br><font color='000000' face='Arial'>");
</script>
 
I am hoping you might still be monitoring this thread because I bumped into an issue I cannot figure out how to resolve in javascript.

There is a slight issue with the script. What happened is that we are going to be closed November 24th. On november 2nd we noticed the script was displaying "holiday" closed when in fact we should have been open.

Upon further investigation what I think is happening is that the expression var ex_day=(rx.test(holidayarray.join(";")))?7:day; is true on November 2 because it is does include all the characters of November 24 (the only difference between November 2 and November 24 is the 4).

Unfortunately I do not have the skill to correct the script, could you graciously give me a hand if you are still monitoring?

Code:
<head>
<script language="javascript">
var mydate=new Date();
var year=mydate.getFullYear();
var day=mydate.getDay();
var month=mydate.getMonth();
var daym=mydate.getDate();
var dayarray=new Array("sunday", "monday","tuesday","wednesday","thursday","friday","saturday");
var montharray=new Array("january","february","march","april","may","june","july","august","september", "october","november","december");

var holidayarray=new Array("May 29","July 4","September 4","November 24");
var rx=new RegExp(montharray[month]+" "+daym,"i");
var ex_day=(rx.test(holidayarray.join(";")))?7:day;
var hourarray=new Array("<font color='#C60000'>Closed Today</font>", "open today from <font color='#C60000'>12pm&nbsp;to&nbsp;8pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;8pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;6pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;8pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;4pm</font>", "open today from <font color='#C60000'>10am&nbsp;to&nbsp;4pm</font>","<font color='#C60000'>Holiday - Closed Today</font>");
document.write (ex_day);
document.write (daym);
document.write("<font face='Arial' size=3>&nbsp;&nbsp;&nbsp;" +hourarray[ex_day]+"<br>&nbsp;&nbsp;&nbsp;<font color='#000000'>"+dayarray[day]+", "+montharray[month]+" "+daym+",  "+year+"<br><font color='000000' face='Arial'>");
</script>
</head>
 
disregard the following commands added to the previous post, they are there while i was testing....

document.write (ex_day);
document.write (daym);

 
Okay, forgotten my involvement in this. That was a bit crude at that time, so it is really a joy to look back into the flaw. It is the delimitation of data that is imperfect. Change these two lines and it will be fine.

>var rx=new RegExp(montharray[month]+" "+daym,"i");
>var ex_day=(rx.test(holidayarray.join(";")))?7:day;

[tt]var rx=new RegExp(montharray[month]+" "+daym[red]+";"[/red],"i");
var ex_day=(rx.test(holidayarray.join(";")[red]+";"[/red]))?7:day;
[/tt]
 
Don't mean to reinvent the wheel, but try this out for size:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
	<body>
		<div id="welcome" style="font-family: Arial; color: #C60000;">&nbsp;</div>
		<script type="text/javascript" language="javascript" charset="utf-8">
			// <![CDATA[
		var mydate=new Date();
		var myweeklyschedule = new Array();
		myweeklyschedule[0] = []; //Sunday
		myweeklyschedule[1] = [12,8]; //Monday
		myweeklyschedule[2] = [10,8]; //Tuesday
		myweeklyschedule[3] = [10,6]; //Wednesday
		myweeklyschedule[4] = [10,8]; //Thursday
		myweeklyschedule[5] = [10,4]; //Friday
		myweeklyschedule[6] = [10,4]; //Saturday

		var myholidayschedule = new Array();
		myholidayschedule[0] = []; //January
		myholidayschedule[1] = []; //Febuary
		myholidayschedule[2] = []; //March
		myholidayschedule[3] = []; //April
		myholidayschedule[4] = []; //May
		myholidayschedule[5] = [29]; //June
		myholidayschedule[6] = []; //July
		myholidayschedule[7] = [4]; //August
		myholidayschedule[8] = []; //September
		myholidayschedule[9] = [4]; //October
		myholidayschedule[10] = [24]; //November
		myholidayschedule[11] = []; //December

		var day=mydate.getDay();
		var htmlstring;
		if(myholidayschedule[mydate.getMonth()].length > 0 && myholidayschedule[mydate.getMonth()].indexOf(mydate.getDate()) >= 0) {
			htmlstring = 'Holiday - Closed Today';
		} else {
			if(myweeklyschedule[day].length > 0) {
				var from = myweeklyschedule[day][0];
				var to = myweeklyschedule[day][1];
				from += from >= 12 ? 'pm' : 'am';
				htmlstring = 'Open Today from ' + from + ' to ' + to + 'pm';
			} else {
				htmlstring = 'Closed Today';
			}
		}
		element = document.getElementById('welcome');
		element.innerHTML = htmlstring;
			// ]]>
		</script>
	</body>
</html>

Let us know your results!

X
 
The following changes worked like a charm:

var rx=new RegExp(montharray[month]+" "+daym+";","i");
var ex_day=(rx.test(holidayarray.join(";")+";"))?7:day;

I cant say I understand the logic of the above statements, and if you found yourself bored and wanted to write a couple of sentences to a newbie (me), knock yourself out :)

The only way this script could be improved in my opinion is that it is dependant upon the date of the computer that is accessing the website. Sometimes they have an inaccurate date on their system which then leads them to believe we are open at a certain time. Since it clearly displays what day it thinks it is, this works fine for us because clearly if it is a different day than what it actually is, the user should be able to catch this....

If there was someway to make the time dependant on the local server, that would be great. Regardless this is a great script that can be used on many websites....
 
To ensure accuracy and consistency, you'd have to insert the date from the server and build off of that. It's not too difficult using any of the server-side scripting languages.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top