Skip to content

Instantly share code, notes, and snippets.

@zackpyle
Created October 3, 2023 15:01
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 zackpyle/bf9368f5e6b430161833d8b1a641d24b to your computer and use it in GitHub Desktop.
Save zackpyle/bf9368f5e6b430161833d8b1a641d24b to your computer and use it in GitHub Desktop.
Convert 24 hour time to AM/PM
// Convert 24 hr times (ex: 11:00:00 - 16:00:00) to AM/PM (11:00am - 4:00pm)
jQuery(document).ready(function ($) {
$('.event-time').each(function() {
const timeRange = $(this).text().split(' - ');
const startTime = convertToAMPM(timeRange[0]);
const endTime = convertToAMPM(timeRange[1]);
const formattedTime = `${startTime} - ${endTime}`;
$(this).text(formattedTime);
});
function convertToAMPM(time24) {
let [hours, minutes, seconds] = time24.split(':');
let period = 'am';
if (hours == 0) {
hours = 12;
} else if (hours == 12) {
period = 'pm';
} else if (hours > 12) {
hours = hours - 12;
period = 'pm';
}
return `${hours}:${minutes}${period}`;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment