Created
October 7, 2014 00:58
-
-
Save zulhfreelancer/a36f257a2e8e4ec16f86 to your computer and use it in GitHub Desktop.
Convert 24 hour time to 12 hour format // Demo: http://jsfiddle.net/tvpt4zgp/
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
<title>24 hour format to 12 hour format</title> | |
</head> | |
<body> | |
<p id="am"></p> | |
<p id="pm"></p> | |
<script type="text/javascript"> | |
var timeString = "11:59"; | |
var H = +timeString.substr(0, 2); | |
var h = (H % 12) || 12; | |
if (h <= 9) { | |
h = '0' + h; | |
} | |
var ampm = H < 12 ? " AM" : " PM"; | |
timeString = h + timeString.substr(2, 3) + ampm; | |
document.getElementById('am').innerHTML = timeString; | |
var timeString = "23:59"; | |
var H = +timeString.substr(0, 2); | |
var h = (H % 12) || 12; | |
if (h <= 9) { | |
h = '0' + h; | |
} | |
var ampm = H < 12 ? " AM" : " PM"; | |
timeString = h + timeString.substr(2, 3) + ampm; | |
document.getElementById('pm').innerHTML = timeString; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment