Skip to content

Instantly share code, notes, and snippets.

@hjst
Last active November 21, 2018 05:22
Show Gist options
  • Save hjst/1326755 to your computer and use it in GitHub Desktop.
Save hjst/1326755 to your computer and use it in GitHub Desktop.
Format a javascript Date object as a 12h am/pm time string.
function format_time(date_obj) {
// formats a javascript Date object into a 12h AM/PM time string
var hour = date_obj.getHours();
var minute = date_obj.getMinutes();
var amPM = (hour > 11) ? "pm" : "am";
if(hour > 12) {
hour -= 12;
} else if(hour == 0) {
hour = "12";
}
if(minute < 10) {
minute = "0" + minute;
}
return hour + ":" + minute + amPM;
}
@Nabeel73
Copy link

it's not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment