Javascript Number formatTime()

Description

Javascript Number formatTime()



Number.prototype.formatTime = function () {
    var hours   = Math.floor(this / 3600),
        minutes = Math.floor((this - (hours * 3600)) / 60),
        seconds = Math.floor(this - (hours * 3600) - (minutes * 60));

    if (minutes < 10) {
        minutes = "0" + minutes;
    }/*from  www .j  a v  a  2  s.  co  m*/
    if (seconds < 10) {
        seconds = "0" + seconds;
    }

    return minutes + ':' + seconds;
}

Javascript Number formatTime()

Number.prototype.formatTime = function() {
 var i = Math.abs(parseInt(this));
 var s = parseInt(i % 60);
 var m = parseInt((i / 60) % 60);
 var h = parseInt((i / 3600) % 60);
 var v = (h>0 ? h + ':' : '') + ((m<10 ? '0' : '') + m + ':') + ((s<10 ? '0' : '') + s);
 if (this < 0) {
  v = '<span class="negative">-' + v + '<span>';
 }
 return v;/* ww  w  .  jav  a  2 s.  com*/
};
Number.prototype.formatNumber = function() {
 var a = Math.abs(this).toString().split('');
 v = '';
 var n = 0;
 while (a.length > 0) {
  if (n>0 && n%3 == 0)
   v = '.' + v;
  v = a.pop() + v;
  n ++;
 }
 if (this < 0) {
  v = '<span class="negative">-' + v + '<span>';
 }
 return v;
};



PreviousNext

Related