Skip to content

Instantly share code, notes, and snippets.

@RedWolves
Forked from paulirish/to12hr.js
Created December 6, 2009 04:26
Show Gist options
  • Save RedWolves/250050 to your computer and use it in GitHub Desktop.
Save RedWolves/250050 to your computer and use it in GitHub Desktop.
var to12Hr = function(n, r /* round to nearest r minutes */) {
if (!n || n >= 24) return '12:00 AM';
var m = (Math.round(n%1*(r = (r ? 60/r : 60)))/r)*60;
return ((n = (m>59 ? n+1 : n))>=13 ? (n|0)-12 : n|0) + ':' + (m>9 ? (m>59 ? '00' : m) : '0'+m) + (n>=12 && m<60 ? ' PM' : ' AM');
}
// to12Hr(6.5) => "6:30 AM"
// to12Hr(13.19) => "1:11 PM"
// to12Hr(13.19, 15) => "1:15 PM" (rounds to 15 mins)
// Useful for jQuery sliders and things.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment