-
-
Save evalcraciun/8eb022f5cef557c1ebc2 to your computer and use it in GitHub Desktop.
Get Current Hour with javascript
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> | |
<html> | |
<body> | |
<p>Click the button to display the hour of the time right now.</p> | |
<button onclick="formatAMPM()">Try it</button> | |
<p id="demo"></p> | |
<p id="demon"></p> | |
<script> | |
//simple current hour | |
function myFunction() { | |
var d = new Date(); | |
var n = d.getHours(); | |
document.getElementById("demo").innerHTML = n; | |
} | |
//current hour with AM/PM | |
function formatAMPM() { | |
var date = new Date(); | |
var hours = date.getHours(); | |
var minutes = date.getMinutes(); | |
var 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; | |
document.getElementById("demo").innerHTML = strTime; | |
} | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment