-1

I found this code but when insert anytime between x:15 - x:45 (x being any associated time) I do not get the intervals for those times.

var setIntervals = function (start, end, inc, oc) {
    start = start.toString().split(':');
    end = end.toString().split(':');
    inc = parseInt(inc, 10);
    oc = oc;

    var pad = function (n) { return (n < 10) ? '0' + n.toString() : n; },
        startHr = parseInt(start[0], 10),
        startMin = parseInt(start[1], 10),
        endHr = parseInt(end[0], 10),
        endMin = parseInt(end[1], 10),
        currentHr = startHr,
        currentMin = startMin,
        previous = currentHr + ':' + pad(currentMin),
        current = '',
        r = [];

    do {
        currentMin += inc;
        if ((currentMin % 60) === 0 || currentMin > 60) {
            currentMin = (currentMin === 60) ? 0 : currentMin - 60;
            currentHr += 1;
        }
        current = currentHr + ':' + pad(currentMin);
        r.push({"end":current, "start":previous, "OpenClosed":oc});
        previous = current;
  } while (currentHr !== endHr);

    return r;
};

var closedTime=setIntervals("<?php echo $close_now ?>","<?php echo $close_end ?>","15", "closed");
var closeArray = [];
closeArray.push(closedTime);

Currently I only get the times from 1:30 - 2:00 but not up to 2:30... If I do 2:00 to 3:00 I get all the intervals.


https://jsfiddle.net/pbbsoxrz/

Added the issue into jsfiddle

Courteous of JavaScript Setting Time Difference through Loop In Array

4
  • I do not get the intervals for those times. what do you get? What did you expect? How are you actually calling it (in Javascript not in php). What are the actual values being passed to the setIntervals function? Did you step through the function to see what it's doing? Commented Dec 30, 2015 at 15:57
  • Ack... Meant to put what I am actually getting in return. Currently I only get the times from 1:30 - 2:00 but not up to 2:30... If I do 2:00 to 3:00 I get all the intervals. Commented Dec 30, 2015 at 16:25
  • Currently I only get the times from 1:30 - 2:00 but not up to 2:30.. when you pass what as an input? 1:15 and 2:45? Or something else? Because your question said x:15 - x:45 from which most people would assume x is that same number in both instances. Commented Dec 30, 2015 at 16:58
  • See edit, I added a jsfiddle Commented Dec 30, 2015 at 17:20

2 Answers 2

1

Just change the while condition and add the part for the minutes with logical or.

while (currentHr !== endHr || currentMin !== endMin);

var setIntervals = function (start, end, inc, oc) {
    start = start.toString().split(':');
    end = end.toString().split(':');
    inc = parseInt(inc, 10);
    oc = oc;

    var pad = function (n) { return (n < 10) ? '0' + n.toString() : n; },
        currentHr = parseInt(start[0], 10),
        currentMin = parseInt(start[1], 10),
        endHr = parseInt(end[0], 10),
        endMin = parseInt(end[1], 10),
        previous = currentHr + ':' + pad(currentMin),
        current = '',
        r = [];

    do {
        currentMin += inc;
        currentHr += currentMin / 60 | 0;
        currentMin %= 60;
        current = currentHr + ':' + pad(currentMin);
        r.push({ start: previous, end: current, OpenClosed: oc });
        previous = current;
    } while (currentHr !== endHr || currentMin !== endMin); // <----------- change this!

    return r;
};

var closedTime = setIntervals("12:15", "14:45", "15", "closed");
var closeArray = [];
closeArray.push(closedTime);

document.write('<pre>' + JSON.stringify(closeArray, 0, 4) + '</pre>');

Sign up to request clarification or add additional context in comments.

Comments

0

Here's what you want:

var setIntervals = function(start, end, inc, oc) {
    var date1 = new Date('2015-1-1 ' + start),
        date2 = new Date('2015-1-1 ' + end),
        r = [],
        current,
        previous;

    // Make sure we increment is a positive number so we don't have an infinite loop
    inc = Math.abs(parseInt(inc, 10));

    do {
        previous = ('0' + date1.getHours()).slice(-2) + ':' + ('0' + date1.getMinutes()).slice(-2);
        date1.setTime(date1.getTime() + inc * 60 * 1000);
        current = ('0' + date1.getHours()).slice(-2) + ':' + ('0' + date1.getMinutes()).slice(-2);
        r.push({
            "end": current,
            "start": previous,
            "OpenClosed": oc
        });
    } while (date1.getTime() < date2.getTime());

    return r;
};

var closeArray = [setIntervals("13:30", "14:30", "15", "closed")];
console.log(closeArray);

The while condition of your original code causes the loop to end every time the two hours are equal.

This approach simplify things a bit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.