Created
March 6, 2011 01:15
-
-
Save danheberden/856911 to your computer and use it in GitHub Desktop.
Gets 4 digit time (hhmm) with optional time frame flooring (e.g. 0317 => 0315 )
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
/* | |
* Returns a 4 digit string of the time in hhmm format | |
* timeFrame will floor to the nearest multiple, e.g. | |
* fourDigitTime( 15, '', 2, 44 ); // returns "0230" | |
* hr and min will use the current time if not specified | |
* sep ( the separator between the hours and minutes ) defaults | |
* to an empty string ('') | |
* | |
* (c) Dan Heberden - danheberden.com | |
* https://gist.github.com/856911 | |
*/ | |
function fourDigitTime( timeFrame, sep, hr, min ) { | |
var d = new Date(), | |
leadZero = function( i ) { return i > 9 ? +i : "0" + +i }; | |
timeFrame = timeFrame || 1; | |
return [ leadZero( hr || d.getHours() ), | |
leadZero( ~~( ( min || d.getMinutes() ) / timeFrame ) * timeFrame ) | |
].join( sep || ''); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment