Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2000
    Messaggi
    1,309

    Sommare n giorni ad una data

    Salve a tutti, mi sapreste dire come afaccio a sommare n giorni ad una data?
    Es.: Mi ricavo la data odierna e vorrei sommargli 20 giorni. A che data corrisponde?
    Codice PHP:
    var strToday = new Date()
        var 
    day strToday.getDate();
        var 
    month strToday.getMonth() + 1;
        var 
    year String(strToday.getFullYear()).substring(2);

        function 
    test() {
            
    alert(strToday.getDate()+30); //mi dice che il girono è 52
            
    alert(day "/" month "/" year);
        } 
    Ho fatto una ricerca nel forum ma niente.
    Ciao By Peter_Pan...

  2. #2
    E' un po macchinoso. Devi convertire il numero di giorni che vuoi sommare in millisecondi, e poi sommarli alla data che vuoi col metodo setTime()

    Trovi tutti i riferimenti sui metodi qui: http://www.w3schools.com/jsref/jsref_obj_date.asp

    Ciao
    Sgro - Webmaster, programmazione c# e progettazione database.
    Non si forniscono soluzioni in privato.
    Non si fornisce la "pappa pronta".

  3. #3
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    alert(strToday.getDate()+30); //mi dice che il girono è 52
    ma qui mica setti i giorni dell' oggetto Date, fai una somma il cui risultato e' ovviamente 52

    puoi usare setDate(), verifica tu stesso
    codice:
      function Data(d){
        return {
          gg:d.getDate(),
          mm:d.getMonth()+1,
          yy:String(d.getFullYear()).substring(2)
        }
      }
    
    var strToday = new Date();
    
    var s=Data(strToday);
    alert(s.gg+'/'+s.mm+'/'+s.yy);
    
    strToday.setDate(strToday.getDate()+20);
    var f=Data(strToday);
    alert(f.gg+'/'+f.mm+'/'+f.yy);
    ciao

  4. #4
    Utente di HTML.it
    Registrato dal
    Jun 2000
    Messaggi
    1,309
    In verità sono riuscito a risolvere, secondo voi va bene??
    Codice PHP:
    var strToday=new Date();
            var 
    strDay;
            var 
    strMonth;
            var 
    strYear;
            
            
    strDay=strToday.getDate();
            
    strMonth=strToday.getMonth();
            
    strYear=strToday.getYear();
            
            
    strDay=strDay+strNumberDayAdd;
            
            
    strToday=new Date(strYear,strMonth,strDay); 
    Ciao By Peter_Pan...

  5. #5
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    si', ma occhio qui
    codice:
    strYear=strToday.getFullYear();
    comunque fossi in te userei il setDate...

  6. #6
    Utente di HTML.it
    Registrato dal
    Jun 2000
    Messaggi
    1,309
    Mi potresti spiegare perchè?? e come modificheresti la stringa con il setDate??
    Ciao By Peter_Pan...

  7. #7
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    se non hai bisogno di creare un nuovo oggetto Date perche' farlo?

    l' esempio e' gia' nell' altro messaggio
    strToday.setDate(strToday.getDate()+20);
    poi fai quello che vuoi con strToday, come fai all' inizio del resto,
    che giorno e'? strDay=strToday.getDate();

    ciao

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.