Javascript Number toHHMMSS()

Description

Javascript Number 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;//from w ww  .j a v a  2  s.  c om
};

Javascript Number toHHMMSS()

Number.prototype.toHHMMSS = function () {
 var hours = Math.floor(this / 3600);
 var minutes = Math.floor((this - (hours * 3600)) / 60);
 var seconds = this - (hours * 3600) - (minutes * 60);
 
 var time = '';
 if (hours > 0) {
  if (hours < 10) {
   time += '0';/*from   www .  j a v  a2s  .c  om*/
  }
  time += hours + ':';
 }
 if (minutes < 10) {
  time += '0';
 }
 time += minutes + ':';
 if (seconds < 10) {
  time += '0';
 }
 time += seconds;
 return time;
};

Javascript Number toHHMMSS()

import React from 'react';

Number.prototype.toHHMMSS = function () {
  let seconds = Math.floor(this),
      hours = Math.floor(seconds / 3600);
  seconds -= hours*3600;/*from   www .  j  av  a  2s  .  c o  m*/
  let minutes = Math.floor(seconds / 60);
  seconds -= minutes*60;

  if (hours   < 10) {hours   = "0"+hours;}
  if (minutes < 10) {minutes = "0"+minutes;}
  if (seconds < 10) {seconds = "0"+seconds;}
  return hours+':'+minutes+':'+seconds;
};

const format = seconds => {
  return seconds.toHHMMSS();
};

Javascript Number toHHMMSS()

var preFix = function(val) {
 return ('' + val).length == 2 ? '' + val : '0' + val;
}

// Convert seconds into HH:MM:SS format
Number.prototype.toHHMMSS = function () {

    var ms = this;
    var decs = Math.floor(ms % 1000 / 100);
    var secs = Math.floor(ms % 60000 / 1000);
    var mins = Math.floor(ms % 3600000 / 60000);
    var hours = Math.floor(ms % (24 * 3600000) / 3600000);

    var time = '';

    if(hours > 0) {
     time += preFix(hours) + ':';
    }/*from   w  w w  .  j  a  v a 2  s .  co m*/
    if(mins > 0) {
     time += preFix(mins) + ':';
    }

    time += preFix(secs) + ':' + preFix(decs);

 return time;
}

// dom prototypes

// remove an entry from element
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

Javascript Number toHHMMSS()

function getRandomInt(min, max) {
    min = Math.ceil(min);/*from   ww w . j  a v  a2s  . c  om*/
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min)) + min;
}

Number.prototype.toHHMMSS = function () {
    var sec_num = this;
    var hours   = Math.floor(sec_num / 3600);
    var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
    var seconds = sec_num - (hours * 3600) - (minutes * 60);
    seconds = seconds.toFixed(2);

    if (hours   < 10) {hours   = "0"+hours;}
    if (minutes < 10) {minutes = "0"+minutes;}
    if (seconds < 10) {seconds = "0"+seconds;}
    return hours+':'+minutes+':'+seconds;
};



PreviousNext

Related