Ho questa funzione x calcolare il numero dei giorni tra 2 date
codice:
<SCRIPT LANGUAGE="JavaScript">
<!--
function isValidDate(dateStr) {
// Checks for the following valid date formats:
// DD/MM/YY DD/MM/YYYY DD-MM-YY DD-MM-YYYY
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]; // parse date into variables
day = matchArray[1];
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 new Date(year,month,day);
}
function dateDiff(dateform) {
date1 = new Date();
date2 = new Date();
diff = new Date();
date1temp=isValidDate(dateform.dal.value)
if (date1temp) { // Validates first date
date1.setTime(date1temp.getTime());
}
else return false; // otherwise exits
date2temp=isValidDate(dateform.al.value)
if (date2temp) { // Validates second date
date2.setTime(date2temp.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.differenza.value = days;
return false; // form should never submit, returns false
}
// End -->
</script>
Se metto 03/09/2007 e 08/09/2007 Va bene
mentre se metto 29/08/2007 e 03/09/2007 mi conta un giorno in meno
come se il mese fosse composto da 30 giorni e non da 31
e questo me lo fa x tutti i mesi che hanno 31 giorni