Skip to content

Instantly share code, notes, and snippets.

@williammalo
Forked from 140bytes/LICENSE.txt
Last active October 2, 2015 16:47
Show Gist options
  • Save williammalo/2278451 to your computer and use it in GitHub Desktop.
Save williammalo/2278451 to your computer and use it in GitHub Desktop.
print time

Returns a string that formats time in a smart manner. If minutes is lower than 10, prepend a "0" to the minutes. If hours is in the afternoon, convert it to PM mode.

Thanks to xpansive for minification tips.

function(
h, //hours
m //minutes
)
{
return
~-h%12+1 //if hours is over 12, subtract 12
+":"+ //add ":"
(
m>9? //if minutes is smaller than 10
"":0 //prepend a "0" to the minutes
)
+(h>12?m+" PM":m) //add minutes, and, if in the afternoon, add "PM"
}
function(h,m){return~-h%12+1+":"+(m>9?"":0)+(h>12?m+" PM":m)}
{
"name": "printTime",
"description": "A time formatter. ",
"keywords": [
"AM/PM",
"time",
"hour",
"format"
]
}
<!DOCTYPE html>
<script>
printTime = function(h,m){return~-h%12+1+":"+(m>9?"":0)+(h>12?m+" PM":m)}
document.write(printTime(new Date().getHours(),new Date().getMinutes()))
</script>
@williammalo
Copy link
Author

@maettig quote:"m>9 check is switched"
Oh! Thanks! It's fixed now :)

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