Skip to content

Instantly share code, notes, and snippets.

@bulatie
Created February 7, 2015 12:32
Show Gist options
  • Save bulatie/cf52bd513d0f21634ff7 to your computer and use it in GitHub Desktop.
Save bulatie/cf52bd513d0f21634ff7 to your computer and use it in GitHub Desktop.
24 hours time format
//pads n with zeros on the left,
//digits is minimum length of output
//zeroPad(3, 5); returns "005"
//zeroPad(2, 500); returns "500"
zeroPad: function(digits, n) {
n = n.toString();
while(n.length < digits)
n = '0' + n;
return n;
},
//it is almost 8 o'clock PM here
//timeString(new Date); returns "19:49"
timeString: function(date) {
var minutes = date.getMinutes().toString();
var hours = date.getHours().toString();
return this.zeroPad(2, hours) + ":" + this.zeroPad(2, minutes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment