Visualizzazione dei risultati da 1 a 10 su 10
  1. #1
    Utente di HTML.it
    Registrato dal
    Apr 2006
    Messaggi
    181

    [js]aiutino per modificare lo script

    Ciao a tutti mi serve un aiutino a modificare questo script. los cript calcola la differenza in giorni tra due date. io vorrei che il formato di inserimento della data non fosse MM/DD/YY ma DD/MM/YY.
    grazie a tutti
    #

    <html xmlns="http://www.w3.org/1999/xhtml" lang="it">
    <head>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function isValidDate(dateStr) {

    // Checks for the following valid date formats:
    // MM/DD/YY MM/DD/YYYY MM-DD-YY MM-DD-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[1]; // parse date into variables
    day = matchArray[3];
    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 true;
    }

    function dateDiff(dateform) {
    date1 = new Date();
    date2 = new Date();
    diff = new Date();

    if (isValidDate(dateform.firstdate.value)) { // Validates first date
    date1temp = new Date(dateform.firstdate.value + " ");
    date1.setTime(date1temp.getTime());
    }
    else return false; // otherwise exits

    if (isValidDate(dateform.seconddate.value)) { // Validates second date
    date2temp = new Date(dateform.seconddate.value + " ");
    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.difference.value = days + " giorni, ";

    return false; // form should never submit, returns false
    }
    // End -->
    </script>
    </head>
    <body >
    <form onSubmit="return dateDiff(this);">
    <table>
    <tr><td>
    <pre>
    Data iniziale: Data: <input type=text name=firstdate value="" size=10 maxlength=10> (formato MM/GG/AAAA)
    Data finale: Data: <input type=text name=seconddate value="" size=10 maxlength=10> (formato MM/GG/AAAA)
    <input type=submit value="Calcola!">

    Differenza:

    <input type=text name=difference value="" size=60>
    </pre>
    </td></tr>
    </table>
    </form>
    </body>
    </html>
    #

  2. #2
    Utente di HTML.it
    Registrato dal
    Apr 2006
    Messaggi
    181
    nessun suggerimento?

  3. #3
    Utente di HTML.it L'avatar di willybit
    Registrato dal
    May 2001
    Messaggi
    4,367
    codice:
    <html xmlns="http://www.w3.org/1999/xhtml" lang="it">
    <head>
    <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.firstdate.value)
    if (date1temp) { // Validates first date 
    date1.setTime(date1temp.getTime());
    }
    else return false; // otherwise exits
    
    date2temp=isValidDate(dateform.seconddate.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.difference.value = days + " giorni, ";
    
    return false; // form should never submit, returns false
    }
    //  End -->
    </script>
    </head>
    <body >
    <form onSubmit="return dateDiff(this);">
    <table>
    <tr><td>
    <pre>
    Data iniziale:   Data: <input type=text name=firstdate value="" size=10 maxlength=10>  (formato GG/MM/AAAA)
                  Data finale:  Data: <input type=text name=seconddate value="" size=10 maxlength=10>  (formato GG/MM/AAAA)
                  <input type=submit value="Calcola!">
    
    Differenza:
    
    <input type=text name=difference value="" size=60>
    </pre>
    </td></tr>
    </table>
    </form>
    </body>
    </html>

  4. #4
    Tiè: (funziona) ... guarda ai commenti verdi
    codice:
    <html xmlns="http://www.w3.org/1999/xhtml" lang="it">
    <head>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function isValidDate(dateStr) {
    // Checks for the following valid date formats:
    // MM/DD/YY MM/DD/YYYY MM-DD-YY MM-DD-YYYY
    // CORRETTO : ammetto solo separatore / 
    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]; // Terzo elemento: mese
    	day = matchArray[1];   // Primo elemento: giorno
    	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 true;
    }
    
    function dateDiff(dateform) {
    var date1 = new Date();
    var date2 = new Date();
    var diff = new Date();
    var arDate,
    	yearDate,
    	monthDate,
    	dayDate;
    	if (isValidDate(dateform.firstdate.value)) { // Validates first date
    		arDate = dateform.firstdate.value.split("/"); faccio un array con giorno,mese,anno
    		yearDate 	= parseInt(arDate[2]);
    		monthDate 	= parseInt(arDate[1]) - 1; // il mese va da 0 a 12
    		dayDate 	= parseInt(arDate[0]);
    		date1temp = new Date(yearDate, monthDate, dayDate);
    		date1.setTime(date1temp.getTime());
    	}
    	else {
    		return false; // otherwise exits
    	}
    
    	if (isValidDate(dateform.seconddate.value)) { // Validates second date
    		arDate = dateform.seconddate.value.split("/"); faccio un array con giorno,mese,anno
    		yearDate 	= parseInt(arDate[2]);
    		monthDate 	= parseInt(arDate[1]) - 1; // il mese va da 0 a 12
    		dayDate 	= parseInt(arDate[0]);
    		date1temp = new Date(yearDate, monthDate, dayDate);
    		date2.setTime(date1temp.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.difference.value = days + " giorni, ";
    	return false; // form should never submit, returns false
    }
    // End -->
    </script>
    </head>
    <body >
    <form onsubmit="return dateDiff(this);">
    <table>
    <tr><td>
    <pre>
    Data iniziale: Data: <input type=text name=firstdate value="" size=10 maxlength=10> (formato GG/MM/AAAA)
    Data finale: Data: <input type=text name=seconddate value="" size=10 maxlength=10> (formato GG/MM/AAAA)
    <input type=submit value="Calcola!">
    
    Differenza:
    
    <input type=text name=difference value="" size=60>
    </pre>
    </td></tr>
    </table>
    </form>
    </body>
    </html>
    HTH
    Zappa
    [PC:Presario 2515EU][Cpu:P4@2.3GHz][Ram: 512M][HDU:80G@5400 RPM]
    [Wireless:LinkSys][OS: Ubuntu 9.04 - Jaunty Jackalope]

  5. #5
    willybit, buona anche la tua, ma hai commesso l'errore sul mese ... a meno che io abbia visto male:
    codice:
    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-1,day);
    }
    ciao
    Zappa
    [PC:Presario 2515EU][Cpu:P4@2.3GHz][Ram: 512M][HDU:80G@5400 RPM]
    [Wireless:LinkSys][OS: Ubuntu 9.04 - Jaunty Jackalope]

  6. #6
    Utente di HTML.it L'avatar di willybit
    Registrato dal
    May 2001
    Messaggi
    4,367
    Originariamente inviato da homezappa
    willybit, buona anche la tua, ma hai commesso l'errore sul mese ... a meno che io abbia visto male:
    hai visto benissimo

  7. #7
    Utente di HTML.it
    Registrato dal
    Apr 2006
    Messaggi
    181
    ciao grazie mille per l'aiuto. mi potresti dire anche come posso fare in modo che il calcolo avvenza senza submit cioè istantaneo chesso tramite onclik o onblur?

  8. #8
    Utente di HTML.it
    Registrato dal
    Apr 2006
    Messaggi
    181
    grazie mille ragazzi dell'aiuto.
    Non ho capito la modifica fatta da zappa, prima funzionava benissimo (perchè ai modificato month con month-1?
    mi sapete dire come posso associare questo script ad un evento piuttosto che ad un submit?
    grazie mille ancora

  9. #9
    Utente di HTML.it L'avatar di willybit
    Registrato dal
    May 2001
    Messaggi
    4,367
    basta togliere l'onsubmit dal form e cambiare il submit con
    codice:
    <input type="button" value="Calcola!" onclick="dateDiff(this.form);">
    in javascript i mesi vanno da 0 a 11 quindi è giustissima la correzione di homezappa

  10. #10
    Utente di HTML.it
    Registrato dal
    Apr 2006
    Messaggi
    181
    ok ti ringrazio sei stato molto gentile

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.