Skip to content

Instantly share code, notes, and snippets.

@chexov
Created June 19, 2017 20:00
Show Gist options
  • Save chexov/4729f6de8284d36d62a6a6da43553556 to your computer and use it in GitHub Desktop.
Save chexov/4729f6de8284d36d62a6a6da43553556 to your computer and use it in GitHub Desktop.
get you Date() into hhmmss.msec
var zeroPad2 = function (n) {
var _n = n + "";
if (_n.length < 2) {
return "0" + _n;
} else {
return _n;
}
};
var zeroPad = function (n, width) {
var z = "0";
var _n = n + "";
return _n.length >= width ? _n : new Array(width - _n.length + 1).join(z) + _n;
};
var timestampToHHmmSS = function(dt){
var hours = Math.trunc(dt.getUTCHours());
var mins = Math.trunc(dt.getUTCMinutes());
var sec = Math.trunc(dt.getUTCSeconds());
var msec = zeroPad(Math.trunc(dt.getUTCMilliseconds()), 3);
return zeroPad2(hours) + ":" + zeroPad2(mins) + ":" + zeroPad2(sec) + "." + msec;
}
var sec = 42;
var dt = new Date(sec * 1000)
timestampToHHmmSS(dt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment