Skip to content

Instantly share code, notes, and snippets.

@acabreragnz
Created November 22, 2016 15:22
Show Gist options
  • Save acabreragnz/45187c2d7799970abc1b0addde6b0fe8 to your computer and use it in GitHub Desktop.
Save acabreragnz/45187c2d7799970abc1b0addde6b0fe8 to your computer and use it in GitHub Desktop.
All the hours in a day in 12-hour clock format with a defined inverval
/**
* All the hours in a day in 12-hour clock format with a defined inverval
*
* @example
* // returns [
* '12:00 AM',
* '12:15 AM',
* '12:30 AM',
* '12:45 AM',
* '01:00 AM',
* ...,
* '11:30 PM'
* '11:45 PM'
* ]
* loadHours(4, 'hh:mm A');
*
* @param hourPartitionSize - the interval of required minutes in an hour (ex: 4 represents 60/4 -> 15 min)
* @param dateFormat - the dateFormat to return in momentjs way
* @returns {Array}
*/
function loadHours(hourPartitionSize, dateFormat) {
hourPartitionSize = _.defaultTo(hourPartitionSize, 4);
dateFormat = _.defaultTo(dateFormat, 'hh:mm A');
var TOTAL_HOURS_IN_DAY = 24;
var TOTAL_MINUTES_IN_HOUR = 60;
var totalIntervals = TOTAL_HOURS_IN_DAY * hourPartitionSize;
var intervalMinute = TOTAL_MINUTES_IN_HOUR / hourPartitionSize;
var mDate = moment().startOf('day');
return _.times(totalIntervals, function() {
var hour = mDate.format(dateFormat);
mDate.add(intervalMinute, 'minutes');
return hour;
});
}
// lodash 4.x
// moment 2.x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment