Skip to content

Instantly share code, notes, and snippets.

@ethanjurman
Created April 24, 2015 13:33
Show Gist options
  • Save ethanjurman/e33bba76e9655c8cbb81 to your computer and use it in GitHub Desktop.
Save ethanjurman/e33bba76e9655c8cbb81 to your computer and use it in GitHub Desktop.
get hours minutes and seconds from a date time
Date.prototype.getHMS = function(){
var hours = (this.getHours() - 19) < 10 ? "0" + (this.getHours() - 19) : (this.getHours() - 19);
var minutes = this.getMinutes() < 10 ? "0" + this.getMinutes() : this.getMinutes();
var seconds = this.getSeconds() < 10 ? "0" + this.getSeconds() : this.getSeconds();
return (hours + ":" + minutes + ":" + seconds);
}
// example
(new Date(70000)).getHMS() // => "00:01:10"
(new Date(800000)).getHMS() // => "00:13:20"
(new Date(1800000)).getHMS() // => "00:30:00"
(new Date(11800000)).getHMS() // => "03:16:40"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment