generate date range base on integers

Home / Uncategorized / generate date range base on integers

Question:
I have an array like this [0,1,2,3 .. 23] and they means hour in a day. How to use moment js to convert them to 12 hour then do date range?

my desire output is
[’12am-1am’,’1am-2am’,’2am-3am’ .. 11pm-12am]

my attempt failed coz I thought I don’t need moment https://jsfiddle.net/s4L7hj1a


Answer:
You don’t need moment.js

var getFormattedHour = function(hour) {
return (hour % 12 ? hour % 12 : 12) + ‘:00 ‘ + ((hour < 12) || (hour >= 24) ? ‘AM’ : ‘PM’);
}

var getHourRange = function(hour) {
return getFormattedHour(hour) + ‘ – ‘ + getFormattedHour(hour + 1);
}

var hours = [];
for(var hour = 0; hour < 24; hour++) { hours.push(hour); } hours.map(function(hour) { console.log(getHourRange(hour)); }); Read more

Leave a Reply

Your email address will not be published. Required fields are marked *