Skip to content

Instantly share code, notes, and snippets.

@chrisjlee
Last active December 19, 2023 20:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chrisjlee/8105283 to your computer and use it in GitHub Desktop.
Save chrisjlee/8105283 to your computer and use it in GitHub Desktop.
In javascript, Convert date object to period or 12 hour notation.
function toPeriodFormat(date) {
var hours = date.getHours(),
minutes = date.getMinutes(),
ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment