ciao a tutti!
ho visto che è un argomento molto trattato ma non ho trovato nulla di specificpo.
io devo sommare dei giorni ad una data e mi sono fatto questa funzione:
specifico che per comodità di inserimento ho messo
che 30 gg = 1 mese , 60 gg = 2 mesi , ... , 360 gg = 1 anno
codice:
//SOMMA UN NUMERO DI GIORNI AD UNA DATA
function SOMdat( DATini , GIOnum ) {
//DATini = DATA INIZIALE A CUI SOMMARE IL NUMERO DI GIORNI
//GIOnum = NUMERO DI GIORNI DA SOMMARE
//IMPOSTAZIONE VARIABILI
var GIOnum = parseInt( GIOnum ) ;
//SPLIT DELLA DATA PASSATA
var DATtmp = DATini.split( "/" ) ;
//IMPOSTAZIONE GIORNO , MESE , ANNO DELLA DATA PASSATA
var GIOtmp = DATtmp[0] ;
var MEStmp = parseInt( DATtmp[1] ) - 1 ;
var ANNtmp = DATtmp[2] ;
//COSTRUZIONE DATA
var NEWdat = new Date( ANNtmp , parseInt( MEStmp , 10 ) , GIOtmp ) ;
//VERIFICA GIOnum MULTIPLO DI 30
if ( ( GIOnum % 30 ) == 0 ) {
//CALCOLO DEI MESI DA SOMMARE
var MESsom = parseInt( ( GIOnum / 30 ) + 1 ) ;
//IMPOSTAZIONI VARIABILI
GIOsom = parseInt( NEWdat.getDate() - 1 ) ;
MESsom = parseInt( NEWdat.getMonth() + MESsom ) ;
ANNsom = NEWdat.getYear() ;
//VERIFICA GIOsom
if ( GIOsom == 0 ) {
TMPdif = MESsom - MEStmp ;
if ( TMPdif > 1 ) {
MESsom = MESsom - 1 ;
GIOsom = ULTgio( MESsom , ANNsom ) ;
}
}
}else{
//SOMMA DEI GIORNI
NEWdat.setDate( NEWdat.getDate() + GIOnum - 1 ) ;
//IMPOSTAZIONI VARIABILI
GIOsom = NEWdat.getDate() ;
MESsom = parseInt( NEWdat.getMonth() ) + 1 ;
ANNsom = NEWdat.getYear() ;
}
//VERIFICA MESsom
while( MESsom > 12 ) {
MESsom = MESsom - 12 ;
ANNsom = ANNsom + 1 ;
}
//ELABORAZIONI RISULTATI
if ( String( GIOsom ).length == 1 ) { GIOsom = "0" + GIOsom ; }
if ( String( MESsom ).length == 1 ) { MESsom = "0" + MESsom ; }
//COSTRUZIONE DATA
SOMdat = GIOsom + "/" + MESsom + "/" + ANNsom ;
//RISULTATO
return SOMdat ;
}
tutto funziona perfettamento tramme che per i mesi di agosto e settembre, ovvero:
01/08/2009 + 12 mesi = 30/11/2009 invece di 31/07/2010
05/08/2009 + 12 mesi = 04/12/2009 invece di 04/08/2010
01/09/2009 + 12 mesi = 30/11/2009 invece di 31/08/2010
13/09/2009 + 12 mesi = 12/12/2009 invece di 12/09/2010
qualche idea???? grz