Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 16
  1. #1

    Restituisce settembre invece che ottobre...

    Ciao perchè questa funzione restituisce come data 1/9/2005???:
    codice:
    <script language=javascript>
    var data = new Date()
    var Anno = data.getYear()
    var Mese = data.getMonth()
    var Giorno = data.getDate()
    var Ora = data.getHours()
    var Minuti = data.getMinutes()
    var Secondi = data.getSeconds()
    DatadiOggi = Giorno + "/" + Mese + "/" + Anno
    document.write("[B]La Data di Oggi è " + DatadiOggi + "
    ")
    OraAttuale = Ora + ":" + Minuti + ":" + Secondi
    document.write("L'Ora Attuale è " + OraAttuale)
    </script>

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    javascript comincia a contare i mesi da 0 (0 = Gennaio, 1 = Febbraio... 11 = Dicembre)
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  3. #3
    Come risolvo? :master:

    Ho scritto così:
    codice:
    var Mese = date.getMonth() +1
    funziona, ma non credo sia esatto...

  4. #4
    Originariamente inviato da Vitacillina
    Come risolvo? :master:
    codice:
    var Mese = data.getMonth()+1;
    ciao

  5. #5
    OK grazie, sarebbe possibile per il giorno avere sempre la doppia cifra, cioè:
    codice:
    01/10/2005
    invece che:
    codice:
    1/10/2005

  6. #6
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Dopo aver recuperato Giorno con
    var Giorno = ....; aggiungi:

    codice:
    Giorno = (Giorno/10 < 1 ? '0'+Giorno: Giorno);
    Comunque, alla fine di ogni istruzione ci va un ;

    codice:
    <script language=javascript>
    var data = new Date();
    var Anno = data.getYear();
    var Mese = data.getMonth();
    var Giorno = data.getDate();
    Giorno = (Giorno/10 < 1 ? '0'+Giorno : Giorno);
    var Ora = data.getHours();
    var Minuti = data.getMinutes();
    var Secondi = data.getSeconds();
    DatadiOggi = Giorno + "/" + Mese + "/" + Anno;
    document.write("[B]La Data di Oggi è " + DatadiOggi + "
    ");
    OraAttuale = Ora + ":" + Minuti + ":" + Secondi;
    document.write("L'Ora Attuale è " + OraAttuale);
    </script>
    alcuni browsers non sono tanto schizzinosi, e lasciano correre, certi... altri semplicemente danno errore e non eseguono lo script
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  7. #7
    Grazie Andrea.

    Questo codice fa parte di un codice più complesso che calcola il fuso orario tra due città, nell'esempio è tra Roma e Los Angeles.

    Però ho due problemi che non riesco a risolvere:

    1) Le date sono incongruenti nel senso che quando a Roma è 01 ottobre ed a L.A. è ancora il 30 settembre (-5 ore), per L.A. viene indicata erroneamente la data del 01 ottobre;

    2) Come è possibile rendere gli orari dinamici, cioè con i secondi che si vedono scorrere sul browser?

    Codice:
    codice:
    <script language="javascript1.2">
    
    <!-- 
    
    // get the date and change it to GMT string
    var date = new Date();
    var timegmt = date.toGMTString();
    
    var Anno = date.getYear();
    var Mese = date.getMonth()+1;
    var Giorno = date.getDate();
    Giorno = (Giorno/10 < 1 ? '0'+Giorno: Giorno);
    DatadiOggi = Giorno + "/" + Mese + "/" + Anno
    
    // split the GMT string at spaces
    time_string = timegmt.split(' ');
    
    // assign variables
    week = time_string[0];
    day = time_string[1];
    mon = time_string[2];
    year = time_string[3];
    hms = time_string[4];
    
    // split the time part on colon
    hms_string = hms.split(':');
    
    // assign variables
    var hour = hms_string[0] - 0;
    var min = hms_string[1];
    
    // convert day-of-week variables to numbers
    if (week == 'Sun,') {
       week = 1
       }
    if (week == 'Mon,') {
       week = 2
       }
    if (week == 'Tue,') {
       week = 3
       }
    if (week == 'Wed,') {
       week = 4
       }
    if (week == 'Thu,') {
       week = 5
       }
    if (week == 'Fri,') {
       week = 6
       }
    if (week == 'Sat,') {
       week = 7
       }
    
    // make array for days of week
    weekly = new Array("Sab", "Dom", "Lun", "Mar", "Mer", "Gio", "Ver", "Sab", "Dom");
       
    //Orario Los Angeles
    var den_hour = hour - 5;
    var den_week = week;
    var den_ampm = " ";
    if (den_hour < 0) {
       den_hour += 24
       den_week -= 1
       }
          
    // Orario Roma
    var lon_hour = hour + 2;
    var lon_week = week;
    var lon_ampm = " ";
    if (lon_hour > 24) {
       lon_hour -= 24
       lon_week += 1
       }
          
    //-->
          
    </script>
    <script language="javascript1.2">
    <!-- hideme
    var loc_hour = date.getHours();
    var loc_min = date.getMinutes();
    var loc_ampm = " a.m.";
    if (loc_hour > 11) {
        loc_ampm = " p.m."
        }
    if (loc_hour > 12) {
        loc_hour -= 12
        }
    //-->
    </script>
    <script language="javascript1.2">
    <!-- hide it
    document.write('Orario Los Angeles
    ');
    document.write(weekly[den_week] + ' - ');
    document.write(DatadiOggi + "
    ")
    document.write(den_hour + ':' + min + den_ampm + '
    ');
    //-->
                        </script>
                        <script language="javascript1.2">
    <!-- hide it
    document.write('Orario Roma (Italia)
    ');
    document.write(weekly[lon_week] + ' - ');
    document.write(DatadiOggi + "
    ")
    document.write(lon_hour + ':' + min + lon_ampm + '
    ');
    //-->
                        </script>

  8. #8
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Non ti correggo lo script perché è tedioso

    comunque il problema è che dovresti sfruttare di più i metodi dell'oggetto Date, che di loro fanno già tutto...

    codice:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <script language="javascript" type="text/javascript">
    
    function fuso(d, i) {
      // i sono le ore di differenza con d;
      return new Date(d.getTime()+i*60*60*1000);
      }
    
    function my_time() {
      
      var date = new Date();
      London = fuso(date,-24);
      Bangkok = fuso(date, 5);
      
      document.writeln("A Roma sono le: "+date.toGMTString()+"
    ");
      document.writeln("A Londra sono le: "+London.toGMTString()+"
    ");
      document.writeln("A Bangkok sono le: "+Bangkok.toGMTString()+"
    ");
    }
      
      
    
    
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Fuso Orario</title>
    </head>
    
    <body onLoad="my_time();">
    </body>
    </html>
    Chiaramente gli orari sono solo dimostrativi, ma come vedrai, il finto orario di Londra cambia anche il giorno nella rappresentazione completa.

    Da un oggetto date (il metodo fuso restituisce un Date) ti tiri fuori le informazioni che vuoi e le formatti secondo i tuoi bisogni.
    Ciao.
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  9. #9
    Grazie, non conoscevo questa tecnica... comunque qui c'è qualcosa che non funziona, ecco cosa restituisce il tuo metodo:
    codice:
    A Roma sono le: Sat, 1 Oct 2005 15:01:18 UTC
    A Londra sono le: Fri, 30 Sep 2005 15:01:18 UTC
    A Bangkok sono le: Sat, 1 Oct 2005 20:01:18 UTC
    A Roma sono le 17:01... :master:

  10. #10
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    scusa, usa: toLocaleString() invece di toGMTString

    Bye bye
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

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 © 2024 vBulletin Solutions, Inc. All rights reserved.