Visualizzazione dei risultati da 1 a 8 su 8

Discussione: Operazione con date

  1. #1
    Utente di HTML.it L'avatar di [trodat]
    Registrato dal
    Oct 2004
    Messaggi
    2,135

    Operazione con date

    Ciao.

    In un form come il seguente:

    codice:
    <html>
    
    <body>
    
    <form method="POST" action="page.asp">
      <input type="text" name="dal_giorno" size="20"> Prima data</p>
      
    
    <input type="text" name="al_giorno" size="20"> Seconda Data</p>
      
    
    <input type="text" name="differenza" size="20"> Giorni di differenza</p>
      
      
    
    <input type="submit" value="Invia" name="B1"></p>
    </form>
    
    </body>
    
    </html>
    Come faccio ad avere in tempo reale, nel campo del form "differenza", il calcolo automatico della differenza tra la prima e la seconda data?

    In ASP faccio così ma è lato server, a me servirebbe lato client:

    codice:
    response.write(DateDiff("d", Date(), "31/12/2002") & "
    ")
    Grazie.
    Lo Stato dà un posto. L’impresa privata dà un lavoro. – Indro Montanelli

  2. #2
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    ho due date, d1 e d2.

    calcolo la differenza delle due date in millisecondi

    differenza = d1.getTime() - d2.getTime();

    in giorni, divido il risultato per

    1000 -> secondi
    60 ->minuti
    60 -> ore
    24 -> ore giorno

    giorni = Math.floor(differenza / (1000*60*60*24));
    Pietro

  3. #3
    Utente di HTML.it L'avatar di [trodat]
    Registrato dal
    Oct 2004
    Messaggi
    2,135
    Scusa non ho capito, mi fai un esempio da adattare al mio form?
    Lo Stato dà un posto. L’impresa privata dà un lavoro. – Indro Montanelli

  4. #4
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    codice:
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="a.aspx.vb" Inherits="prove_a" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Pagina senza titolo</title>
        
    <script language="javascript" type="text/javascript">
    // <!CDATA[
    /*---------------------------------------------------------------------
    Questa funzione accetta una variabile stringa e verifica se e una data.
    La data deve essere nel formato giorno mese anno. Il giorno e il mese
    devono essere di 1 o 2 cifre, l'anno deve essere di 2 o 4 cifre.
    Il delimitatore deve essere / o - o .
    Restituisce un oggetto con le proprieta:
    	.isdate = vero o falso
    	.error	= messaggio errore
    	.year	= anno
    	.month	= mese (1 - 12)
    	.day	= giorno (1 - 31)
    	.data   = oggetto data
    ---------------------------------------------------------------------*/
    function isdate(dateStr) {
    	
    	var ret = new Object();
    	ret.isdate = false;
    	ret.error = "";
    	ret.year = 0;
    	ret.month = 0;
    	ret.day = 0;
    	ret.data = null;
    	
    	
        // inizio stringa
        // 1 o 2 cifre
        // / o - o .
        // 1 o 2 cifre
        // / o - o .
    	// 4 cifre
    	// fine stringa    
        //var datePat = /^(\d{1,2})(\/|-|.)(\d{1,2})(\/|-|.)(\d{4})$/;
        var datePat = /^\s*(\d{1,2})(\/|-|\.)(\d{1,2})(\2)(\d{2}|\d{4})\s*$/
        
        //restituisce, allo stesso modo di una matrice, i risultati di una ricerca
        //di una stringa utilizzando un oggetto Regular Expression
        var matchArray = dateStr.match(datePat); 
    
        if (matchArray == null) {
            ret.error = "Per favore, introduci la data nei formati gg/mm/aaaa o gg-mm-aaaa o gg.mm.aaaa";
            return ret;
        }
    
        day = matchArray[1];
        month = matchArray[3]; 
        year = matchArray[5];
        if(year.length == 2)
    		if(parseInt(year,10) < 30)
    			year = parseInt(year,10) + 2000;
    		else
    			year = parseInt(year,10) + 1900;
    
        if (month < 1 || month > 12) { // il mese deve essere compreso tra 1 e 12
            ret.error = "Il mese deve essere compreso tra 1 e 12.";
            return ret;
        }
    
        if (day < 1 || day > 31) {//il giorno non puo essere < 1 e > 31
            ret.error = "Il giorno deve essere compreso tra 1 e 31";
            return ret;
        }
    
        if ((month==4 || month==6 || month==9 || month==11) && day==31) {
            //i mesi aprile, giugno, settembre, novembre, hanno 30 giorni
            ret.error = "Il mese " + month + " non ha 31 giorni!";
            return ret;
        }
    
        if (month == 2) { // verifica se l'anno e bisestile: febbraio puo avere 29 giorni
            var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
            if (day > 29 || (day==29 && !isleap)) {
    			ret.error = "Febbraio " + year + " non ha " + day + " giorni!";
    			return ret;
            }
        }
        isDateError = "";
        // se sono qui, la data e valida!
    	ret.isdate = true;
    	ret.error = "";
    	ret.year = year;
    	ret.month = month;
    	ret.day = day;
    	ret.data = new Date(year, (month - 1), day);
        
        return ret;
    }
    
    function calcola_giorni()
    {
        var d1 = isdate(document.getElementById("dal_giorno").value);
        var d2 = isdate(document.getElementById("al_giorno").value);
        if(d1.isdate && d2.isdate)
        {
            var differenza = d1.data.getTime() - d2.data.getTime();
            var giorni = Math.floor(differenza /(1000*60*60*24));
            document.getElementById("differenza").value = giorni;
        }
        else 
        {
            document.getElementById("differenza").value = "";
            alert("date non valide");
        }
    }
    
    // ]]>
    </script>
    </head>
    <body >
        <form id="form1" runat="server">
            
    
    
                <input type="text" id="dal_giorno" name="dal_giorno" size="20"/> 
                Prima data
            </p>
            
    
    
                <input type="text" id="al_giorno" name="al_giorno" size="20"/> 
                Seconda Data</p>
            
    
    
                <input type="text" id="differenza" name="differenza" size="20"/>
                Giorni di differenza<
            /p>
            
    
    
                <input type="button" value="Invia" id="button1" name="button1" onclick="calcola_giorni();"/>
            </p>
    
        </form>
    </body>
    </html>
    Pietro

  5. #5
    Utente di HTML.it L'avatar di [trodat]
    Registrato dal
    Oct 2004
    Messaggi
    2,135
    grazie, ma qualcosa non mi è chiaro...

    1) se digito la data 19/07/2006 nel campo dal_giorno e digito la data 20/07/2006 nel campo al_giorno il calcolo restituisce valore -1... non dovrebbe essere 1 (giorno trascorso) ???

    2) sono costretto cmq a cliccare sul pulsante per avere la differenza, mi servirebbe la compilazione automatica all'interno del campo differenza...
    Lo Stato dà un posto. L’impresa privata dà un lavoro. – Indro Montanelli

  6. #6
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    Originariamente inviato da [trodat]
    grazie, ma qualcosa non mi è chiaro...

    1) se digito la data 19/07/2006 nel campo dal_giorno e digito la data 20/07/2006 nel campo al_giorno il calcolo restituisce valore -1... non dovrebbe essere 1 (giorno trascorso) ???

    2) sono costretto cmq a cliccare sul pulsante per avere la differenza, mi servirebbe la compilazione automatica all'interno del campo differenza...

    eh! lo sapevo

    1) ti debbo pure suggerire di invertire
    var differenza = d1.data.getTime() - d2.data.getTime();
    e mettere
    var differenza = d2.data.getTime() - d1.data.getTime();

    ma dai, su... :maLOL:


    2) ti debbo pure suggerire di utilizzare l'evento onchange al posto del click sul pulsante?
    Pietro

  7. #7
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    prova così
    codice:
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="a.aspx.vb" Inherits="prove_a" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Pagina senza titolo</title>
        
    <script language="javascript" type="text/javascript">
    // <!CDATA[
    /*---------------------------------------------------------------------
    Questa funzione accetta una variabile stringa e verifica se e una data.
    La data deve essere nel formato giorno mese anno. Il giorno e il mese
    devono essere di 1 o 2 cifre, l'anno deve essere di 2 o 4 cifre.
    Il delimitatore deve essere / o - o .
    Restituisce un oggetto con le proprieta:
    	.isdate = vero o falso
    	.error	= messaggio errore
    	.year	= anno
    	.month	= mese (1 - 12)
    	.day	= giorno (1 - 31)
    	.data   = oggetto data
    ---------------------------------------------------------------------*/
    function isdate(dateStr) {
    	
    	var ret = new Object();
    	ret.isdate = false;
    	ret.error = "";
    	ret.year = 0;
    	ret.month = 0;
    	ret.day = 0;
    	ret.data = null;
    	
    	
        // inizio stringa
        // 1 o 2 cifre
        // / o - o .
        // 1 o 2 cifre
        // / o - o .
    	// 4 cifre
    	// fine stringa    
        //var datePat = /^(\d{1,2})(\/|-|.)(\d{1,2})(\/|-|.)(\d{4})$/;
        var datePat = /^\s*(\d{1,2})(\/|-|\.)(\d{1,2})(\2)(\d{2}|\d{4})\s*$/
        
        //restituisce, allo stesso modo di una matrice, i risultati di una ricerca
        //di una stringa utilizzando un oggetto Regular Expression
        var matchArray = dateStr.match(datePat); 
    
        if (matchArray == null) {
            ret.error = "Per favore, introduci la data nei formati gg/mm/aaaa o gg-mm-aaaa o gg.mm.aaaa";
            return ret;
        }
    
        day = matchArray[1];
        month = matchArray[3]; 
        year = matchArray[5];
        if(year.length == 2)
    		if(parseInt(year,10) < 30)
    			year = parseInt(year,10) + 2000;
    		else
    			year = parseInt(year,10) + 1900;
    
        if (month < 1 || month > 12) { // il mese deve essere compreso tra 1 e 12
            ret.error = "Il mese deve essere compreso tra 1 e 12.";
            return ret;
        }
    
        if (day < 1 || day > 31) {//il giorno non puo essere < 1 e > 31
            ret.error = "Il giorno deve essere compreso tra 1 e 31";
            return ret;
        }
    
        if ((month==4 || month==6 || month==9 || month==11) && day==31) {
            //i mesi aprile, giugno, settembre, novembre, hanno 30 giorni
            ret.error = "Il mese " + month + " non ha 31 giorni!";
            return ret;
        }
    
        if (month == 2) { // verifica se l'anno e bisestile: febbraio puo avere 29 giorni
            var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
            if (day > 29 || (day==29 && !isleap)) {
    			ret.error = "Febbraio " + year + " non ha " + day + " giorni!";
    			return ret;
            }
        }
        isDateError = "";
        // se sono qui, la data e valida!
    	ret.isdate = true;
    	ret.error = "";
    	ret.year = year;
    	ret.month = month;
    	ret.day = day;
    	ret.data = new Date(year, (month - 1), day);
        
        return ret;
    }
    
    function calcola_giorni()
    {
        if(document.getElementById("dal_giorno").value.length == 0 || document.getElementById("al_giorno").value.length == 0) return;
        
        var d1 = isdate(document.getElementById("dal_giorno").value);
        var d2 = isdate(document.getElementById("al_giorno").value);
        
        if(d1.isdate && d2.isdate)
        {
            var differenza = d2.data.getTime() - d1.data.getTime();
            var giorni = Math.floor(differenza /(1000*60*60*24));
            document.getElementById("differenza").value = giorni;
        }
        else 
        {
            document.getElementById("differenza").value = "";
            alert("date non valide");
        }
    }
    
    
    window.onload = function()
    {
        document.getElementById("dal_giorno").onchange = calcola_giorni;
        document.getElementById("al_giorno").onchange = calcola_giorni;
    }
    
    // ]]>
    </script>
    </head>
    <body >
        <form id="form1" runat="server">
            
    
    
                <input type="text" id="dal_giorno" name="dal_giorno" size="20"/> 
                Prima data
            </p>
            
    
    
                <input type="text" id="al_giorno" name="al_giorno" size="20"/> 
                Seconda Data</p>
            
    
    
                <input type="text" id="differenza" name="differenza" size="20"/>
                Giorni di differenza<
            /p>
            
    
    
                <input type="submit" value="Invia" id="button1" name="button1" />
            </p>
    
        </form>
    </body>
    </html>
    Pietro

  8. #8
    Utente di HTML.it L'avatar di [trodat]
    Registrato dal
    Oct 2004
    Messaggi
    2,135
    Perfetto, grazie
    Lo Stato dà un posto. L’impresa privata dà un lavoro. – Indro Montanelli

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.