Ver Mensaje Individual
  #5 (permalink)  
Antiguo 12/08/2011, 06:53
Avatar de ThunderWolf
ThunderWolf
 
Fecha de Ingreso: julio-2011
Mensajes: 30
Antigüedad: 13 años, 3 meses
Puntos: 1
Sonrisa Respuesta: Cronómetro PRECISO a partir de fecha

¡Aleluya! ¡Alabado sea Google!

Por fin mis oraciones han sido escuchadas; no las estaba rezando en el idioma apropiado, el ingles. Harto de hacer combinaciones de búsqueda en español, probé a buscar lo mismo pero con tags anglosajones. Descartando los tropecientos códigos basados en segundos, encontré la solución definitiva en el siguiente maravilloso script:

Código Javascript:
Ver original
  1. <script type="text/javascript">
  2. /**********************************************************************************************
  3. * CountUp script by Praveen Lobo (http://PraveenLobo.com/techblog/javascript-countup-timer/)
  4. * This notice MUST stay intact(in both JS file and SCRIPT tag) for legal use.
  5. * http://praveenlobo.com/blog/disclaimer/
  6. **********************************************************************************************/
  7. function CountUp(initDate, id){
  8.     this.beginDate = new Date(initDate);
  9.     this.countainer = document.getElementById(id);
  10.     this.numOfDays = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
  11.     this.borrowed = 0, this.years = 0, this.months = 0, this.days = 0;
  12.     this.hours = 0, this.minutes = 0, this.seconds = 0;
  13.     this.updateNumOfDays();
  14.     this.updateCounter();
  15. }
  16.  
  17. CountUp.prototype.updateNumOfDays=function(){
  18.     var dateNow = new Date();
  19.     var currYear = dateNow.getFullYear();
  20.     if ( (currYear % 4 == 0 && currYear % 100 != 0 ) || currYear % 400 == 0 ) {
  21.         this.numOfDays[1] = 29;
  22.     }
  23.     var self = this;
  24.     setTimeout(function(){self.updateNumOfDays();}, (new Date((currYear+1), 1, 2) - dateNow));
  25. }
  26.  
  27. CountUp.prototype.datePartDiff=function(then, now, MAX){
  28.     var diff = now - then - this.borrowed;
  29.     this.borrowed = 0;
  30.     if ( diff > -1 ) return diff;
  31.     this.borrowed = 1;
  32.     return (MAX + diff);
  33. }
  34.  
  35. CountUp.prototype.calculate=function(){
  36.     var currDate = new Date();
  37.     var prevDate = this.beginDate;
  38.     this.seconds = this.datePartDiff(prevDate.getSeconds(), currDate.getSeconds(), 60);
  39.     this.minutes = this.datePartDiff(prevDate.getMinutes(), currDate.getMinutes(), 60);
  40.     this.hours = this.datePartDiff(prevDate.getHours(), currDate.getHours(), 24);
  41.     this.days = this.datePartDiff(prevDate.getDate(), currDate.getDate(), this.numOfDays[currDate.getMonth()]);
  42.     this.months = this.datePartDiff(prevDate.getMonth(), currDate.getMonth(), 12);
  43.     this.years = this.datePartDiff(prevDate.getFullYear(), currDate.getFullYear(),0);
  44. }
  45.  
  46. CountUp.prototype.addLeadingZero=function(value){
  47.     return value < 10 ? ("0" + value) : value;
  48. }
  49.  
  50. CountUp.prototype.formatTime=function(){
  51.     this.seconds = this.addLeadingZero(this.seconds);
  52.     this.minutes = this.addLeadingZero(this.minutes);
  53.     this.hours = this.addLeadingZero(this.hours);
  54. }
  55.  
  56. CountUp.prototype.updateCounter=function(){
  57.     this.calculate();
  58.     this.formatTime();
  59.     this.countainer.innerHTML ="<strong>" + this.years + "</strong> <small>" + (this.years == 1? "year" : "years") + "</small>" +
  60.         " <strong>" + this.months + "</strong> <small>" + (this.months == 1? "month" : "months") + "</small>" +
  61.         " <strong>" + this.days + "</strong> <small>" + (this.days == 1? "day" : "days") + "</small>" +
  62.         " <strong>" + this.hours + "</strong> <small>" + (this.hours == 1? "hour" : "hours") + "</small>" +
  63.         " <strong>" + this.minutes + "</strong> <small>" + (this.minutes == 1? "minute" : "minutes") + "</small>" +
  64.         " <strong>" + this.seconds + "</strong> <small>" + (this.seconds == 1? "second" : "seconds") + "</small>";
  65.     var self = this;
  66.     setTimeout(function(){self.updateCounter();}, 1000);
  67. }
  68.  
  69. window.onload=function(){ new CountUp('April 16, 2010 19:00:00', 'counter'); }
  70.  
  71. </script>

Como se puede apreciar, no solo distingue entre los meses de 30, 31, y 28 días; ademas toma en consideración los años bisiestos. También utiliza el plural o el singular de la medida según corresponda (en ingles, pero es fácilmente modificable). Y no solo eso; ademas muestra sus resultados en un div para meterle el CSS que más te convenga. :

Código HTML:
Ver original
  1. <div id="counter">Lo que metas aquí aparece solo si hay problemas con el script.</div>

En el sitio de donde lo he extraído (incluido en el script) figuran también contadores regresivos, progresivos como el de este post, o una mezcla de ambos. Ademas indica como poner varios de ellos en la misma página.

El código es tan sencillo que hasta yo que soy negado en Javascript he conseguido añadirle los milisegundos por mi cuenta. Añadiría también las décimas, pero creo que no existe un get. para tal efecto.

Estoy más feliz que un regaliz

Última edición por ThunderWolf; 12/08/2011 a las 09:53 Razón: Perfeccionismo