Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Aug 2004
    Messaggi
    6

    gestione date in javascript

    Ciao a tutti!
    Premetto che non ho competenze nel linguaggio javascript e qualche volta sono riuscita a modificare con successo alcuni script.
    Adesso mi trovo di fronte ad un problema: ho trovato questo codice su internet, ma non riesco a modificare l'ordine mm/dd/yyyy in dd/mm/yyyy.

    Qualcuno può aiutarmi?

    Grazie mille!

    Riporto il codice:

    <SCRIPT LANGUAGE="JavaScript">






    <!-- Begin
    function isValidDate(dateStr) {
    // Date validation function courtesty of
    // Sandeep V. Tamhankar (stamhankar@hotmail.com) -->

    // 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("La data non è stata inserita nel giusto formato")
    return false;
    }
    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[4];
    if (month < 1 || month > 12) { // check month range
    alert("Il mese deve essere compreso tra 1 e 12");
    return false;
    }
    if (day < 1 || day > 31) {
    alert("Il giorno deve essere compreso tra 1 e 31");
    return false;
    }
    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
    alert("Il mese "+month+" non è di 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 dispDate(dateObj) {
    month = dateObj.getMonth()+1;
    month = (month < 10) ? "0" + month : month;

    day = dateObj.getDate();
    day = (day < 10) ? "0" + day : day;

    year = dateObj.getYear();
    if (year < 2000) year += 1900;

    return (month + "/" + day + "/" + year);
    }

    function pregnancyCalc(pregform) {
    menstrual = new Date(); // creates new date objects
    ovulation = new Date();
    duedate = new Date();
    today = new Date();
    cycle = 0, luteal = 0; // sets variables to invalid state ==> 0

    if (isValidDate(pregform.menstrual.value)) { // Validates menstual date
    menstrualinput = new Date(pregform.menstrual.value);
    menstrual.setTime(menstrualinput.getTime())
    }
    else return false; // otherwise exits

    cycle = (pregform.cycle.value == "" ? 28 : pregform.cycle.value); // defaults to 28
    // validates cycle range, from 22 to 45
    if (pregform.cycle.value != "" && (pregform.cycle.value < 22 || pregform.cycle.value > 45)) {
    alert("La lunghezza del tuo ciclo è troppo corta o troppo lunga per \n"
    + "un calcolo accurato! E' necessario riprovare \n"
    + "inserendo un valore compreso tra 22 e 45 ");
    }

    luteal = (pregform.luteal.value == "" ? 14 : pregform.luteal.value); // defaults to 14
    // validates luteal range, from 9 to 16
    if (pregform.luteal.value != "" && (pregform.luteal.value < 9 || pregform.luteal.value > 16)) {
    alert("La lunghezza della fase luteale è troppo corta o lunga per \n"
    + "un calcolo accurato! E' necessario riprovare \n"
    + "inserendo un valore compreso tra 9 e 16 ");
    }

    // sets ovulation date to menstrual date + cycle days - luteal days
    // the '*86400000' is necessary because date objects track time
    // in milliseconds; 86400000 milliseconds equals one day
    ovulation.setTime(menstrual.getTime() + (cycle*86400000) - (luteal*86400000));
    pregform.conception.value = dispDate(ovulation);

    // sets due date to ovulation date plus 266 days
    duedate.setTime(ovulation.getTime() + 266*86400000);
    pregform.duedate.value = dispDate(duedate);

    // sets fetal age to 14 + 266 (pregnancy time) - time left
    var fetalage = 14 + 266 - ((duedate - today) / 86400000);
    weeks = parseInt(fetalage / 7); // sets weeks to whole number of weeks
    days = Math.floor(fetalage % 7); // sets days to the whole number remainder

    // fetal age message, automatically includes 's' on week and day if necessary
    fetalage = weeks + " settimane" + (weeks > 1 ? "" : "") + ", " + days + " giorni";
    pregform.fetalage.value = fetalage;

    return false; // form should never submit, returns false
    }
    // End -->
    </script>

  2. #2
    codice:
    ...
    month = matchArray[3]; // parse date into variables
    day = matchArray[1];
    year = matchArray[4];
    ...
    Edit:
    ed anche:
    codice:
    function dispDate(dateObj) {
    month = dateObj.getMonth()+1;
    month = (month < 10) ? "0" + month : month;
    
    day = dateObj.getDate();
    day = (day < 10) ? "0" + day : day;
    
    year = dateObj.getYear();
    if (year < 2000) year += 1900;
    
    return (day + "/" + month + "/" + year);
    }
    HTH
    Zappa
    [PC:Presario 2515EU][Cpu:P4@2.3GHz][Ram: 512M][HDU:80G@5400 RPM]
    [Wireless:LinkSys][OS: Ubuntu 9.04 - Jaunty Jackalope]

  3. #3
    Utente di HTML.it
    Registrato dal
    Aug 2004
    Messaggi
    6
    Grazie mille!
    Adesso provo a modificare il codice!


  4. #4
    Utente di HTML.it
    Registrato dal
    Aug 2004
    Messaggi
    6
    ho fatto una prova col vecchio codice per verificare che i risultati del programmino siano gli stessi con il nuovo codice, ma con i parametri di data corretti .... ma il risultato è diverso.

    Il link alla pagina di prova col vecchio codice é:
    http://www.puntocol.it/ginecologo_ci..._ostetrico.htm

    il link alla pagina col codice modificato é:
    http://www.puntocol.it/ginecologo_ci...rico_prova.htm



    sai darmi qualche altro suggerimento???


  5. #5
    Utente di HTML.it
    Registrato dal
    Aug 2004
    Messaggi
    6
    Ciao a tutti!
    Sono rimasta col medesimo problema della settimana scorsa!
    Nessuno sa darmi una mano?

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.