Skip to content

Instantly share code, notes, and snippets.

@dannyid
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannyid/d808600aabb6d967fe5d to your computer and use it in GitHub Desktop.
Save dannyid/d808600aabb6d967fe5d to your computer and use it in GitHub Desktop.
Format Time
function formatTime(time) {
//time needs to be a Date object like 'new Date()'
var h = time.getHours(),
m = time.getMinutes(),
s = time.getSeconds(),
m = (m < 10 ? '0' : '' ) + m,
s = (s < 10 ? '0' : '' ) + s,
formattedTime = (h > 12 ? h - 12 : (h === 0 ? h + 12 : h)) + ':' + m + ':' + s + (h >= 12 ? ' PM': ' AM');
return formattedTime;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment