rAthus icon

JavaScript timer live elapsed time from timestamp

rAthus | PRO | 05/17/23 09:56:19 AM UTC (Edited) | 0 ⭐ | 1086 👁️ | Never ⏰ | []
JavaScript |

940 B

|

None

|

0 👍

/

0 👎

// example : <span since="578556600"></span>
 
function updateSince($this) {
    var ts_since = $this.attr('since')*1;
    var ts_now = Math.round(Date.now()/1000);
    var s_total = Math.max(ts_now-ts_since,0);
    var y = Math.floor(s_total/31536000);
    var m = Math.floor(s_total/2592000);
    var d = Math.floor(s_total/86400);
    var h = Math.floor(s_total/3600);
    var i = Math.floor((s_total-h*3600)/60);
    var s = s_total-h*3600-i*60;
    if (y)
        $this.html(y+' year'+((y>1)?'s':''));
    else if (m)
        $this.html(m+' month'+((m>1)?'s':''));
    else if (d)
        $this.html(d+' day'+((d>1)?'s':''));
    else if (h)
        $this.html(h+' hour'+((h>1)?'s':''));
    else if (i)
        $this.html(i+' minute'+((i>1)?'s':''));
    else if (s>20)
        $this.html('a few moments');
    else
        $this.html('a few seconds');
}
jQuery('[since]').each(function() {
    var $this = jQuery(this);
    updateSince($this);
    var interval = setInterval(function() {
        updateSince($this);
    },1000);
});

Comments