ms to hh:mm:ss time format (v6)

Revision 6 of this benchmark created on


Description

Testing popular answers to SO question http://stackoverflow.com/questions/6312993/javascript-seconds-to-time-with-format-hhmmss

Setup

var simpleFormatter = function(ms) {
      var seconds = parseInt(ms / 1000);
      var hh = Math.floor(seconds / 3600);
      var mm = Math.floor((seconds - (hh * 3600)) / 60);
      var ss = seconds - (hh * 3600) - (mm * 60);
    
      if (hh < 10) {
        hh = '0' + hh
      }
      if (mm < 10) {
        mm = '0' + mm
      }
      if (ss < 10) {
        ss = '0' + ss
      }
    
      return hh + ':' + mm + ':' + ss;
    };
    
    var regExpFormatter = function(ms) {
      return new Date(ms).toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1");
    };
    
    var dateObjFormatter = function(ms) {
      var date = new Date(ms);
    
      var hh = date.getHours();
      var mm = date.getMinutes();
      var ss = date.getSeconds();
    
      if (hh < 10) {
        hh = '0' + hh
      }
      if (mm < 10) {
        mm = '0' + mm
      }
      if (ss < 10) {
        ss = '0' + ss
      }
    
      return hh + ':' + mm + ':' + ss;
    };

Test runner

Ready to run.

Testing in
TestOps/sec
simpleFormatter
simpleFormatter(+new Date());
ready
regExpFormatter
regExpFormatter(+new Date());
ready
dateObjFormatter
dateObjFormatter(+new Date());
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.