Created
October 3, 2023 15:01
-
-
Save zackpyle/bf9368f5e6b430161833d8b1a641d24b to your computer and use it in GitHub Desktop.
Convert 24 hour time to AM/PM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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