Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

15 minute interval between two dates javascript

Problem: I need to create an array of all the 15 minute time slots between two date/time stamps (date format: 2016-08-10 16:00:00) into a HH:mm format where the minutes are restricted to 00,15,30,45.

Example: 12:30 PM to 2:30 PM would be > 12:30,12:45,13:00,13:15,13:30,13:45,14:00,14:15

Code below was working but it didn't take into account the starting timeslot (it always started at '00'). Code was adjusted to account for it but now it won't correctly increment the hour.

function generateTimeSlots(s,e)
{   // create an array of timeslots (15 min interval) between 2 dates

var ckTimes = new Array();
var timeSlots = new Array();
var diff = Math.abs((new Date(e)) - (new Date(s)));
var duration = Math.floor((diff/1000)/60);
var sHour = (((s.split(' ')[1]).split(':'))[0]) * 1;
var ampm = (s.split(' '))[2];

// convert hour to 24 hour
if(ampm+'' == 'PM' ${AND} sHour != 12)
{
    sHour = sHour + 12;
}

var hours = (duration / 60);
var slots = (hours * 4); // total 15 min slots

var chunk = ['00','15','30','45'] // the four 15 min slots in a hour

// Attempt to move starting position to the correct 15 min block
// Beforehand, it would always start at the '00' slot
// i.e. 12:30 pm to 1:30pm would return 12:00,12:15,12:30,12:45
var i = "";
var startMin = (((s.split(' ')[1]).split(':'))[1])
// note platform doesn't support indexOf
for (var z = 0; 4 > z; z++){
    if (chunk[z].toString() == startMin.toString())
        i = z;
}

slots += i;

for(i;slots > i;i++)
{
    var quarter = (i % 4);
    var sMin = chunk[quarter];
    var slot = sHour+":"+sMin;
    if(ampm+'' == 'AM' ${AND} 10 > sHour)
    {
        slot = "0"+slot;
    }
    timeSlots.push(slot)

    if(hours > 1)
    {
        if(quarter == 3)
        {
            sHour++;
        }
    }
}

return timeSlots;
}
like image 932
Cary Trusty Avatar asked Aug 26 '16 16:08

Cary Trusty


2 Answers

Might I suggest moment.js? It makes tasks like this much easier:

function intervals(startString, endString) {
    var start = moment(startString, 'YYYY-MM-DD hh:mm a');
    var end = moment(endString, 'YYYY-MM-DD hh:mm a');

    // round starting minutes up to nearest 15 (12 --> 15, 17 --> 30)
    // note that 59 will round up to 60, and moment.js handles that correctly
    start.minutes(Math.ceil(start.minutes() / 15) * 15);

    var result = [];

    var current = moment(start);

    while (current <= end) {
        result.push(current.format('YYYY-MM-DD HH:mm'));
        current.add(15, 'minutes');
    }

    return result;
}

console.log(intervals('2016-08-10 4:35:00 PM', '2016-08-10 8:06:00 PM'));

// Output:
// [ '2016-08-10 16:45',
//   '2016-08-10 17:00',
//   '2016-08-10 17:15',
//   '2016-08-10 17:30',
//   '2016-08-10 17:45',
//   '2016-08-10 18:00',
//   '2016-08-10 18:15',
//   '2016-08-10 18:30',
//   '2016-08-10 18:45',
//   '2016-08-10 19:00',
//   '2016-08-10 19:15',
//   '2016-08-10 19:30',
//   '2016-08-10 19:45',
//   '2016-08-10 20:00' ]
like image 161
user94559 Avatar answered Nov 04 '22 18:11

user94559


A JS version.

https://jsfiddle.net/justindevs/wgk63o1y/

//Input
var startTime = "2016-08-10 02:30:00"
var endTime = "2016-08-10 04:45:00"

//Parse In
var parseIn = function(date_time){
    var d = new Date();
  d.setHours(date_time.substring(11,13));
    d.setMinutes(date_time.substring(14,16));

  return d;
}

//make list
var getTimeIntervals = function (time1, time2) {
    var arr = [];
  while(time1 < time2){
    arr.push(time1.toTimeString().substring(0,5));
    time1.setMinutes(time1.getMinutes() + 15);
  }
  return arr;
}

startTime = parseIn(startTime);
endTime = parseIn(endTime);

var intervals = getTimeIntervals(startTime, endTime);
like image 26
justindevs Avatar answered Nov 04 '22 19:11

justindevs