Nodejs Time Format formatTime(seconds)

Here you can find the source of formatTime(seconds)

Method Source Code

function formatTime(seconds) {
  if (seconds < 0) return "";

  var minutes = 0;
  var hours = 0;//ww w  .j ava  2 s . c  o  m
  var days = 0;
  var weeks = 0;
  if (seconds >= 60) {
    minutes = Math.floor(seconds/60);
    seconds = seconds-minutes*60;
  }
  if (minutes >= 60) {
    hours = Math.floor(minutes/60);
    minutes = minutes-hours*60;
  }
  if (hours >= 24) {
    days = Math.floor(hours/24);
    hours = hours-days*24;
  }
  if (days >= 7) {
    weeks = Math.floor(days/7);
    days -= weeks*7;
  }
  var res = "";
  var resCounter=0;

  if (weeks > 0 && resCounter < 2) {
    res += weeks+"w ";
    resCounter++;
  }
  if (days > 0 && resCounter < 2) {
    res += days+"d ";
    resCounter++;
  }
  if (hours > 0 && resCounter < 2) {
    res += hours+"h ";
    resCounter++;
  }
  if (minutes > 0 && resCounter < 2) {
    res += minutes+"m ";
    resCounter++;
  }
  if (seconds > 0 && resCounter < 2) {
    res += seconds+"s ";
    resCounter++;
  }
  return res;
}

Related

  1. 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;
    };
    
  2. formatTime(forceMinutes, forceHours)
    Number.prototype.formatTime = function (forceMinutes, forceHours) {
        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 = '';
        if (forceHours || h > 0) {
            v += h + ':';
        if (v || forceMinutes || m > 0) {
            if (v && m < 10) {
                v += '0';
            v += m + ':';
        if (v && s < 10) {
            v += '0';
        v += s;
        if (this < 0) {
            v = '<span class="negative">-' + v + '<span>';
        return v;
    };
    
  3. toHHMMSS()
    Number.prototype.toHHMMSS = function () {
      var hours   = Math.floor(this / 3600);
      var minutes = Math.floor((this - (hours * 3600)) / 60);
      var seconds = Math.round(this - (hours * 3600) - (minutes * 60));
      if (hours   < 10) {hours   = "0"+hours;}
      if (minutes < 10) {minutes = "0"+minutes;}
      if (seconds < 10) {seconds = "0"+seconds;}
      var time    = hours+':'+minutes+':'+seconds;
      return time;
    ...
    
  4. toHHMMSS()
    Number.prototype.toHHMMSS = function () {
      var sec_num = parseInt(this, 10);
      var hours   = Math.floor(sec_num / 3600);
      var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
      var seconds = sec_num - (hours * 3600) - (minutes * 60);
      if (hours   < 10) {hours   = "0" + hours;}
      if (minutes < 10) {minutes = "0" + minutes;}
      if (seconds < 10) {seconds = "0" + seconds;}
      return hours + ':' + minutes + ':' + seconds;
    ...