indexzero
6/29/2017 - 6:33 PM

Generate a 24 hour range of 30-minute time intervals that are i18n compatible

Generate a 24 hour range of 30-minute time intervals that are i18n compatible

let items = [];
for (var hour = 0; hour < 24; hour++) {
  items.push([hour, 0]);
  items.push([hour, 30]);
}

const date = new Date();
const formatter = new Intl.DateTimeFormat('en-US', {
  hour: 'numeric',
  minute: 'numeric',
  hour12: false
});

const range = items.map(time => {
  const [hour, minute] = time;
  date.setHours(hour);
  date.setMinutes(minute);

  return formatter.format(date);
});

console.dir(range);