Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    Non 1, 2, 3 ma 01, 02 ,03!

    <head>
    <script LANGUAGE="Javascript">
    <!--
    function dateandtime(){
    var dayNames = new Array("Domenica","Lunedì","Martedì","Mercoledì","G iovedì","Venerdì","Sabato")
    var monthNames = new Array("Gennaio","Febbraio","Marzo","Aprile","Maggi o","Giugno","Luglio","Agosto","Settembre","Ottobre ","Novembre","Dicembre")
    var now = new Date()
    var year =now.getYear()
    h = now.getMinutes();
    h = now.getMinutes();
    var H;
    if (h < 10)
    H = "0" + h;
    else
    H = h;[/B]

    document.getElementById('orologio').innerHTML = dayNames[now.getDay()] + " " + now.getDate() + " " + monthNames[now.getMonth()] + " " + year + " ore " + now.getHours() + ":" + H + ":" + s +now.getSeconds()
    }
    // -->
    </script>

    </head>
    <body onload="dateandtime();setInterval('dateandtime()', '1000')">
    <table>
    <tr>
    <td id="orologio" style="font-family:Verdana; font-size:10px; font-style: normal;text-align:center" rowspan="3"></td>
    </tr>
    </table>
    </body>

    CON LE MODIFICHE I HGRASSETTO CHE VEDETE HO FATTO IN MODO CHE LO ZETRO NEL CAMPO DEI MINUTI, CHE PRIMA MI CALCOLAVA COME null E NON VENIVA VISUALIZZATO, ORA SI VEDA.
    LO STESSO PROBLEMA PERO' CE L'HO ADESSO CON I SECONDI.
    HO PROVATO A FARE UNA MODIFICA SIMILE PER I SECONDI MA NON E' ANDATA.
    QUALCUNO SAPREBBE COME AIUTARMI?
    CAPITO IL PROBLEMA?
    EVITARE CHE I SECONDI CHE VANNO DA 0 A 9 NON VENGANO VISUALIZZATI COME 1,2,3,4,5 ETC. MA 01,02,03,04,05 ETC.

    HELP ME GUYS!
    @lex!

  2. #2
    Moderatore di JavaScript L'avatar di br1
    Registrato dal
    Jul 1999
    Messaggi
    19,998
    il codice che hai postato e' un po' "sporco"...

    suggerimento:

    String(100+now.getSeconds()).substr(1)

    ciao
    Il guaio per i poveri computers e' che sono gli uomini a comandarli.

    Attenzione ai titoli delle discussioni: (ri)leggete il regolamento
    Consultate la discussione in rilievo: script / discussioni utili
    Usate la funzione di Ricerca del Forum

  3. #3
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    ma perchè apri + di una discussione x problemi praticamente identici?
    l' altra volta ne ho unite 2,
    oggi hai anche quest' altra...
    x riportare su una discussione basta rispondersi da soli!

  4. #4
    nosferatu_net
    Guest
    Sono combattuto: da un lato cerco di non rispondere a chi fa cross-browsing, dall'altro ho recentemente avuto a che fare con un problema identico al tuo, e ho risolto (come mia perversa abitudine) con un prototipo JavaScript per gestire l'intera classe di problemi, anziché il problema specifico.
    codice:
    var f = new SimpleDateFormat("EEEE d MMMM yyyy 'ore' HH:ss");
    document.getElementById('orologio').innerHTML = f.format(new Date());
    Facile no?

    Butta questo codice in una libreria JS da caricare in esterno e il gioco è fatto:
    codice:
    /**
     * java.text.SimpleDateFormat
     * Si usa allo stesso identico modo di:
     * http://java.sun.com/j2se/1.4.1/docs/...ateFormat.html
     */
    
    var LOCALIZED_MONTHS = new Array(
      "Gennaio", "Febbraio", "Marzo", "Aprile",
      "Maggio", "Giugno", "Luglio", "Agosto",
      "Settembre", "Ottobre", "Novembre", "Dicembre"
    );
    
    var LOCALIZED_DAYS = new Array(
      "Domenica", "Lunedì",
      "Martedì", "Mercoledì",
      "Giovedì", "Venerdì", "Sabato"
    );
    
    String.prototype.nf = function(c)
    {
      return this.length < c ? ("0" + this).nf(c) : this;
    }
    
    String.prototype.isBrokenApart = function()
    {
      if (this.length < 2)
        return (this.length < 1 || this < 'A' || this > 'z');
      return false;
    }
    
    Array.prototype.ss = function(i, exp, func)
    {
      var it = this[i].split(exp);
      if (it.length < 2)
        return this[i].isBrokenApart();
      eval("this.splice(i, 1, '" + it.join("',func,'") + "')");
      return this[i].isBrokenApart();
    }
    
    function _yyy(d) {return d.getFullYear()};
    function _yy(d) {return ("" + d.getFullYear()).substring(2)};
    function _y(d) {return d.getFullYear()};
    function _MMMM(d) {return LOCALIZED_MONTHS[d.getMonth()]};
    function _MMM(d) {return LOCALIZED_MONTHS[d.getMonth()].substring(0, 3)};
    function _MM(d) {return ("" + (d.getMonth() + 1)).nf(2)};
    function _M(d) {return d.getMonth() + 1};
    function _dd(d) {return ("" + d.getDate()).nf(2)};
    function _d(d) {return d.getDate()};
    function _EEEE(d) {return LOCALIZED_DAYS[d.getDay()]};
    function _EEE(d) {return LOCALIZED_DAYS[d.getDay()].substring(0, 3)};
    function _EE(d) {return ("" + d.getDay()).nf(2)};
    function _E(d) {return d.getDay()};
    function _HH(d) {return ("" + d.getHours()).nf(2)};
    function _H(d) {return d.getHours()};
    function _mm(d) {return ("" + d.getMinutes()).nf(2)};
    function _m(d) {return d.getMinutes()};
    function _ss(d) {return ("" + d.getSeconds()).nf(2)};
    function _s(d) {return d.getSeconds()};
    function _SSS(d) {return ("" + d.getMilliseconds()).nf(3)};
    function _SS(d) {return ("" + d.getMilliseconds()).nf(2)};
    function _S(d) {return d.getMilliseconds()};
    function _a(d) {return d.getHours() < 12 ? "AM" : "PM"};
    function _KK(d) {return d.getHours() <= 12 ? d.getHours() : ("" + (d.getHours() - 12)).nf(2)};
    function _K(d) {return d.getHours() <= 12 ? d.getHours() : d.getHours() - 12};
    
    function SimpleDateFormat(pattern)
    {
      var ps = (" " + pattern + " ").split("'");
      if (ps.length % 2 == 0) return;
      for (var i = 1; i < ps.length - 1; i++)
      {
        if (ps[i] == "")
        {
          ps[i-1] += "'" + ps[i+1];
          ps.splice(i--, 2);
        }
      }
    
      for (var i = 0; i < ps.length; i+=2)
        if
        (
          ps[i].isBrokenApart()
          || ps.ss(i, /y{3,}/, _yyy)
          || ps.ss(i, "yy", _yy)
          || ps.ss(i, "y", _y)
          || ps.ss(i, /M{4,}/, _MMMM)
          || ps.ss(i, "MMM", _MMM)
          || ps.ss(i, "MM", _MM)
          || ps.ss(i, "M", _M)
          || ps.ss(i, /d{2,}/, _dd)
          || ps.ss(i, "d", _d)
          || ps.ss(i, /E{4,}/, _EEEE)
          || ps.ss(i, "EEE", _EEE)
          || ps.ss(i, "EE", _EE)
          || ps.ss(i, "E", _E)
          || ps.ss(i, /H{2,}/, _HH)
          || ps.ss(i, "H", _H)
          || ps.ss(i, /m{2,}/, _mm)
          || ps.ss(i, "m", _m)
          || ps.ss(i, /s{2,}/, _ss)
          || ps.ss(i, "s", _s)
          || ps.ss(i, /S{3,}/, _SSS)
          || ps.ss(i, "SS", _SS)
          || ps.ss(i, "S", _S)
          || ps.ss(i, /a+/, _a)
          || ps.ss(i, /K{2,}/, _KK)
          || ps.ss(i, "K", _K)
        )
        continue;
      
      ps[0] = ps[0].substring(1);
      pl = ps.length - 1;
      ps[pl] = ps[pl].substring(0, ps[pl].length - 1);
      this.ps = ps;
    }
    
    SimpleDateFormat.prototype.format = function(d)
    {
      var r = "";
      for (var i = 0; i < this.ps.length; i++)
        if (typeof this.ps[i] == "function")
          r += this.ps[i](d);
        else
          r += this.ps[i];
      return r;
    }

  5. #5
    nosferatu_net
    Guest
    Mi correggo: per questioni prestazionali ti consiglio di non scrivere come ti dicevo sopra, bensì:
    codice:
    var f = new SimpleDateFormat("EEEE d MMMM yyyy 'ore' HH:ss");
    
    function dateandtime() {
      document.getElementById('orologio').innerHTML = f.format(new Date());
    }
    ...cioè l'oggetto formattante ti consiglio di istanziarlo una volta sola, e invocare il metodo format tutte le volte.
    L'istanza mangia qualche ms (70 sul mio PII 233), mentre l'invocazione metodo è praticamente istantanea (0 ms da me).

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.