Skip to content

Instantly share code, notes, and snippets.

@danheberden
Created March 6, 2011 01:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danheberden/856911 to your computer and use it in GitHub Desktop.
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 )
/*
* 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