Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 19
  1. #1
    Utente di HTML.it L'avatar di kodode
    Registrato dal
    Sep 2002
    Messaggi
    1,896

    [date] non somma nei mesi di agosto e settembre

    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

  2. #2
    Utente di HTML.it L'avatar di Pastore12
    Registrato dal
    Oct 2008
    Messaggi
    1,051
    Se devi sommare dei giorni a una Data

    codice:
    var data = new Date (2009,01,14);
    data.setDate(data.getDate()+numGiorni);
    Ciao!
    "Ethics are to me something private. Whenever you use it as an argument for why somebody_else should do something, you’re no longer being ethical, you’re just being a sanctimonious dick-head"
    Linus Torvalds

  3. #3
    Utente di HTML.it L'avatar di kodode
    Registrato dal
    Sep 2002
    Messaggi
    1,896
    grazie per la risposta ma se vedi quando sommo dei giorni uso proprio
    codice:
                             //SOMMA DEI GIORNI
    		NEWdat.setDate( NEWdat.getDate() + GIOnum - 1 ) ;
    		
    		//IMPOSTAZIONI VARIABILI
    		GIOsom = NEWdat.getDate()                  ;
    		MESsom = parseInt( NEWdat.getMonth() ) + 1 ;
    		ANNsom = NEWdat.getYear()                  ;
    il mio problema c'è quando sommo dei mesi
    ossia
    codice:
                             //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 ) ;
    			}
    		}
    nel caso specifico se somma 12 mesi ho :
    02/07/2009 -> 01/07/2010 -->ok
    02/08/2009 -> 01/12/2009 -->no
    02/09/2009 -> 01/12/2009 -->no
    02/10/2009 -> 01/10/2010 -->ok

    ok?

  4. #4
    Utente di HTML.it L'avatar di Pastore12
    Registrato dal
    Oct 2008
    Messaggi
    1,051
    ok?
    no. non riesco proprio a capire il motivo per cui fai tutto questo casino.
    Ti occorre una funzione che aggiunga giorni a una data? la funzione esiste già.
    Cos'altro devi fare?
    "Ethics are to me something private. Whenever you use it as an argument for why somebody_else should do something, you’re no longer being ethical, you’re just being a sanctimonious dick-head"
    Linus Torvalds

  5. #5
    Utente di HTML.it L'avatar di kodode
    Registrato dal
    Sep 2002
    Messaggi
    1,896
    allora riscrivo

    in base a quello che viene inserito o sommo dei GIORNI o sommo dei MESI

    mi intoppo quando sommo dei mesi... tutto qui...

  6. #6
    Utente di HTML.it L'avatar di kodode
    Registrato dal
    Sep 2002
    Messaggi
    1,896
    sono risalito a questo:

    var NEWdat = new Date( 2009 , 07 , 01 ) ;
    alert(NEWdat.getMonth()) ;

    questo mi restituisce 6

    var NEWdat = new Date( 2009 , 08 , 01 ) ;
    alert(NEWdat.getMonth()) ;

    questo mi restituisce 11

    var NEWdat = new Date( 2009 , 09 , 01 ) ;
    alert(NEWdat.getMonth()) ;

    questo mi restituisce 11

    var NEWdat = new Date( 2009 , 10 , 01 ) ;
    alert(NEWdat.getMonth()) ;

    questo mi restituisce 9

    quindi luglio e ottobre sono giusti
    agosto e settembre no. qualche idea?

  7. #7
    Utente di HTML.it L'avatar di Pastore12
    Registrato dal
    Oct 2008
    Messaggi
    1,051
    allora, ricapitoliamo. Se quello che devi fare è un esercizio scolastico, va bene. La consegna dice di fare così e tu fai così.
    Ma se non è la consegna a importi di fare questa cosa, forse sarebbe meglio usare la funzione setDate() che almeno in teoria fa le cose in modo non meno performante di come le faresti tu o chiunque altro.

    A questo punto sono costretto a pensare che ti sia stato dato come esercizio. Va bene.
    Non ho mai visto uno stile come il tuo e questo mi crea non pochi problemi a comprendere quello che stai facendo con il tuo codice.

    Io vengo da java e conosco javascript da molto poco, però mi è sempre apparso che le convenzioni per i nomi usate in javascript sono molto simili alle convenzioni di java, quindi nomi di variabili e metodi tutti in minuscolo con stile cammellato per le parole composte.
    Un'altra bella tecnica di programmazione che vale la pena imparare è quella di usare nomi di variabili autoespicativi. Se sei costretto a spiegare il significato delle variabili, non ci siamo.

    Ad esempio esasperato:
    codice:
    function sommaGiorniAData( strDataInizio , numGiorni ) {
      ...
    Ora, la semplificazione dei 30 giorni per mesi è una tua semplificazione? In tal caso sono sicuro che il codice si può riscrivere in modo più elegante.
    E' una semplificazione temporanea? Allora chi ti ha imposto questa cosa non è troppo sano di mente.

    Dunque, io ti posso dare una mano con il pseudocodice. I dati di partenza (dopo lo split della data) sono:
    - giornoDataInizio
    - meseDataInizio
    - annoDataInizio
    - numeroGiorni

    ricavare il numero di mesi è assurdo. Tra l'altro, se usi ora setDate() allora tutto questo esercizio è una idiozia. Lo usavi all'inizio e in una riga avevi finito.

    dovresti procedere in questo modo:
    codice:
    giorniTotali = giornoDataInizio + numeroGiorni;
    giornoDataFine = giorniTotali % 30;
    se (giornoDataFine==0)
      giornoDataFine = 30;
    
    mesiTotali = meseDataInizio +  (giorniTotali-giornoDataFine) / 30;
    meseDataFine = mesiTotali % 12;
    se (meseDataFine==0)
      meseDataFine = 12;
    
    annoDataFine = annoDataInizio + (mesiTotali - meseDataFine) /12;
    dataFine = new Date(annoDataFine, meseDataFine-1, giornoDataFine);
    Spero che se ci sono errori di logica è più semplice trovarli qui che nel tuo codice.
    Sono sicuro poi, che una volta ideato l'algoritmo corretto, non avrai problemi a scriverlo in javascript.

    P.S.: Beh... adesso che hai un problema semplice da capire e concreto il discorso cambia..
    "Ethics are to me something private. Whenever you use it as an argument for why somebody_else should do something, you’re no longer being ethical, you’re just being a sanctimonious dick-head"
    Linus Torvalds

  8. #8
    Utente di HTML.it L'avatar di Pastore12
    Registrato dal
    Oct 2008
    Messaggi
    1,051
    quindi luglio e ottobre sono giusti agosto e settembre no. qualche idea?
    A me il codice che segue funzioni perfettamente.

    Testato su Win XP con Firefox e Explorer

    Codice PHP:
    <html>
    <
    head>
        <
    script language="javascript">
        var 
    NEWdat = new Date2009 07 01 ) ;
        
    alert("new Date( 2009 , 07 , 01 ) "+NEWdat.getMonth()) ;
        
        
    NEWdat = new Date2009 08 01 ) ;
        
    alert("new Date( 2009 , 08 , 01 ) "+NEWdat.getMonth()) ;

        
    NEWdat = new Date2009 09 01 ) ;
        
    alert("new Date( 2009 , 09 , 01 ) "+NEWdat.getMonth()) ;

        
    NEWdat = new Date2009 10 01 ) ;
        
    alert("new Date( 2009 , 10 , 01 ) "+NEWdat.getMonth()) ;
        
    </script>
    </head>
    <body>
    </body>
    </html> 
    "Ethics are to me something private. Whenever you use it as an argument for why somebody_else should do something, you’re no longer being ethical, you’re just being a sanctimonious dick-head"
    Linus Torvalds

  9. #9
    Utente di HTML.it L'avatar di kodode
    Registrato dal
    Sep 2002
    Messaggi
    1,896
    ascolta io sono felicissimo che tu abbia scritto tutta la lezione ma non era questo il problema

    non devo fare nessuna lezione
    ho trovato il mio problema e stava proprio nel codice ( che bastava leggere e rileggere)
    quando creo new Date
    ho messo ( e non ho idea del perchè)

    codice:
    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]                 ;
    
    ....
    in particolare
    var MEStmp = parseInt( DATtmp[1] ) - 1 ;

    questa quando trovava DATtmp[1] = 09 oppure 08
    restituiva -1

    è bastato eliminare parseInt
    quindi

    //IMPOSTAZIONE GIORNO , MESE , ANNO DELLA DATA PASSATA
    var GIOtmp = DATtmp[0] ;
    var MEStmp = DATtmp[1] - 1 ;
    var ANNtmp = DATtmp[2] ;

    e TUTTO funziona...

    grazie a me stesso per l'aiuto...

  10. #10
    Utente di HTML.it L'avatar di Pastore12
    Registrato dal
    Oct 2008
    Messaggi
    1,051
    Complimenti a te stesso allora!

    Ma per qualche strana ragione.. sono convinto che accettare qualche consiglio su come scrivere codice ti sarebbe molto utile.
    "Ethics are to me something private. Whenever you use it as an argument for why somebody_else should do something, you’re no longer being ethical, you’re just being a sanctimonious dick-head"
    Linus Torvalds

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.