Skip to content

Instantly share code, notes, and snippets.

@monkeycycle
Created November 27, 2011 22:12
Show Gist options
  • Save monkeycycle/1398263 to your computer and use it in GitHub Desktop.
Save monkeycycle/1398263 to your computer and use it in GitHub Desktop.
JavaScript format Seconds value as Formatted Time 00:00:00
function formatSecondsAsTime(secs, format) {
var hr = Math.floor(secs / 3600);
var min = Math.floor((secs - (hr * 3600))/60);
var sec = Math.floor(secs - (hr * 3600) - (min * 60));
if (hr < 10) { hr = "0" + hr; }
if (min < 10) { min = "0" + min; }
if (sec < 10) { sec = "0" + sec; }
if (hr) { hr = "00"; }
if (format != null) {
var formatted_time = format.replace('hh', hr);
formatted_time = formatted_time.replace('h', hr*1+""); // check for single hour formatting
formatted_time = formatted_time.replace('mm', min);
formatted_time = formatted_time.replace('m', min*1+""); // check for single minute formatting
formatted_time = formatted_time.replace('ss', sec);
formatted_time = formatted_time.replace('s', sec*1+""); // check for single second formatting
return formatted_time;
} else {
return hr + ':' + min + ':' + sec;
}
}
@MatLeRock
Copy link

Variable hr will always be "00" in this code.
I think code in line 9 shoulde be if (!hr) { hr = "00"; }

@johnbahamon
Copy link

00:00:00,000 ???

@morfair
Copy link

morfair commented Dec 12, 2016

where days?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment