Icontem

File: getHoliday-en.html

Recommend this page to a friend!
  Classes of Pierre FAUQUE  >  JavaScript Holiday Dates  >  getHoliday-en.html  >  Download  
File: getHoliday-en.html
Role: Example script
Content type: text/plain
Description: Example of use (getHoliday) for english users
Class: JavaScript Holiday Dates
Calculator for the dates of different holidays
Author: By
Last change:
Date: 4 years ago
Size: 2,769 bytes
 

Contents

Class file image Download
<html>

<head>
<title>DateObj.getHoliday()</title>

<script language="javascript" type="text/javascript">
// For english users:
Date.prototype.getHoliday=function(v){var Y,y,a,b,c,D,d,e,f,g,M,m,p,v,s;var i=new Array(-45,-41,0,39,49);var s="ashleneasascwhi";var p=s.indexOf(v.substring(0,3).toLowerCase())/3;Y=''+this.getFullYear();y=Y.substr(2,2);a=Y%19;b=Y%4;c=Y%7;e=((19*a)+24)%30;f=((2*b)+(4*c)+(6*e)+5)%7;g=(22+e+f);h=new Date(Y,2,g+i[p],0,0,0);d=h.getDate();D=(d<10)?'0'+d:d;m=h.getMonth()+1;M=(m<10)?'0'+m:m;return M+'/'+D+'/'+Y;};
</script>

<script language="javascript" type="text/javascript">
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Test the validity of a French date (DD/MM/YYYY).
// Return "OK" or an error message
// You can interchange the lines A and B (with indices correction) for a format such as MM/DD/YYYY
function isValidDate(date) {
	var date, tab, reg = new RegExp("^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$","g");
	if(reg.test(date)) {
		tab=date.split('/');
		if((tab[0]*1)<1 || (tab[0]*1)>31) { return "Incorrect day"; }   // A
		if((tab[1]*1)<1 || (tab[1]*1)>12) { return "Incorrect month"; } // B
		return "OK";
	}
	return "Incorrect format date";
}

function showHoliday() {

	var holiday, vacation, date, tab, j, m, a, thedate, msg;

	holiday = document.test.holiday.options[document.test.holiday.options.selectedIndex].value;
	vacation = document.test.holiday.options[document.test.holiday.options.selectedIndex].text;
	date = document.test.date.value;
	tab = date.split('/');

	j=tab[0]*1; m=tab[1]-1; a=tab[2]*1;
	thedate = new Date(a, m, j);

	msg = vacation+' '+thedate.getFullYear()+' arrives on '+thedate.getHoliday(holiday);
	alert(msg);
}

function verif() {

	var thedate, msg;

	// get the values of the form
	thedate = document.test.date.value;

	if(!thedate) {
		alert('Date is missing');
		document.test.date.focus();
		return false;
	}

	msg = isValidDate(thedate);
	if(msg != "OK") {
		alert("Date :\n"+msg);
		document.test.date.focus();
		return false;
	}

	return true;
}
</script>
</head>

<body>

<h3>Movable holidays</h3>

<form name="test" method="post" action="javascript:showHoliday()" onsubmit="return verif()">
Write an undistinguished date (DD/MM/YYYY) <input type="text" name="date" size="10"><br/>
For this year, what movable holiday do you want ?
<select name="holiday">
  <option value="Ash">Ash</option>
  <option value="Lent">Lent</option>
  <option value="Easter">Easter</option>
  <option value="Ascension">Ascension</option>
  <option value="Whitesun">Whitesun</option>
</select><br/>
<input type="submit" name="submit" value="Validate">
</form>

</body>

</html>