Tiè: (funziona) ... guarda ai commenti verdi
codice:
<html xmlns="http://www.w3.org/1999/xhtml" lang="it">
<head>
<SCRIPT LANGUAGE="JavaScript">
<!--
function isValidDate(dateStr) {
// Checks for the following valid date formats:
// MM/DD/YY MM/DD/YYYY MM-DD-YY MM-DD-YYYY
// CORRETTO : ammetto solo separatore /
var datePat = /^(\d{1,2})(\/)(\d{1,2})\2(\d{4})$/; // requires 4 digit year
var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert(dateStr + " Formato della data non valido.")
return false;
}
month = matchArray[3]; // Terzo elemento: mese
day = matchArray[1]; // Primo elemento: giorno
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Mese: deve essere un valore compreso tra 1 e 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Giorno: deve essere un valore compreso tra 1 e 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Il mese "+month+" non ha 31 giorni!")
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("Febbraio " + year + " non ha" + day + " giorni!");
return false;
}
}
return true;
}
function dateDiff(dateform) {
var date1 = new Date();
var date2 = new Date();
var diff = new Date();
var arDate,
yearDate,
monthDate,
dayDate;
if (isValidDate(dateform.firstdate.value)) { // Validates first date
arDate = dateform.firstdate.value.split("/"); faccio un array con giorno,mese,anno
yearDate = parseInt(arDate[2]);
monthDate = parseInt(arDate[1]) - 1; // il mese va da 0 a 12
dayDate = parseInt(arDate[0]);
date1temp = new Date(yearDate, monthDate, dayDate);
date1.setTime(date1temp.getTime());
}
else {
return false; // otherwise exits
}
if (isValidDate(dateform.seconddate.value)) { // Validates second date
arDate = dateform.seconddate.value.split("/"); faccio un array con giorno,mese,anno
yearDate = parseInt(arDate[2]);
monthDate = parseInt(arDate[1]) - 1; // il mese va da 0 a 12
dayDate = parseInt(arDate[0]);
date1temp = new Date(yearDate, monthDate, dayDate);
date2.setTime(date1temp.getTime());
}
else {
return false; // otherwise exits
}
// sets difference date to difference of first date and second date
diff.setTime(Math.abs(date1.getTime() - date2.getTime()));
timediff = diff.getTime();
days = Math.floor(timediff / (1000 * 60 * 60 * 24));
dateform.difference.value = days + " giorni, ";
return false; // form should never submit, returns false
}
// End -->
</script>
</head>
<body >
<form onsubmit="return dateDiff(this);">
<table>
<tr><td>
<pre>
Data iniziale: Data: <input type=text name=firstdate value="" size=10 maxlength=10> (formato GG/MM/AAAA)
Data finale: Data: <input type=text name=seconddate value="" size=10 maxlength=10> (formato GG/MM/AAAA)
<input type=submit value="Calcola!">
Differenza:
<input type=text name=difference value="" size=60>
</pre>
</td></tr>
</table>
</form>
</body>
</html>
HTH
Zappa