开发者

trying to find the amount of half hours in between two times

I can't think right now.... i've been trying to figure out how many half hours there are between two variables, which will have the start and end times like so, 1100 and 1430

I started doing the following but realized time doesnt go to 100!

function process_blocks(startTime,endTime){

var blocks = 0;

startTime = +startTime;
endTime = +endTime;

while(startTime < endTime){
startTime = startTime + 30; // add a half hour, which is a block
blocks++;
}
return blocks;

}
开发者_StackOverflow中文版


function getHalfHourCount(start, end) {
    start = getMinutes(start);
    end = getMinutes(end);
    var diff;
    if (end < start)
        diff = 24*60 - start + end
    else 
        diff = end - start;

    if (start % 30 != 0)
        diff = diff + start % 30;

    --diff;

    var result = Math.floor(diff / 30);

    document.write(result + "<br />");
    return result;
}

function getMinutes(time) {
    time = String(time);
    if (time.length == 4)
        return parseInt(time.substr(0, 2)) * 60 + parseInt(time.substr(2, 2));
    else
        return parseInt(time.substr(0, 1)) * 60 + parseInt(time.substr(1, 2));
}

see my working demo: http://jsfiddle.net/roberkules/pUfF2/


You need to treat the thousands & hundreds numbers as hours, and the tens & ones numbers as minutes.

24 hour times always have 4 digits, so take the first two, and compare:

$first = 11;
$second = 14;

$diff = $second - $first; //number of full hours

$fmin = 00;
$smin = 30;

$sdiff = $smin - $fmin; // number of minutes difference
//Do magic to determine how many half hours this is, probably using division and modulo
$halfs = magic($sdiff);

$halfhours = (2 * $diff) + $halfs;


Calculate the interval in minutes then just divide by 30:

/**
 * @param startTime integer hours and minutes as an integer like 1100 or 1430
 * @param endTime integer hours and minutes as an integer like 1100 or 1430
 * endTime must be equal to or higher than startTime, else you'll get a negative
 * number of half hours :D
 */
function half_hours_in_interval(startTime, endTime) {
    var hours, minutes, total_minutes;

    hours = Math.floor(endTime / 100) - Math.floor(startTime / 100);
    minutes = (endTime % 100) - (startTime % 100);

    if (minutes < 0) { // this step is probably not even necessary, but just for sanity's sake
        hours -= 1;
        minutes += 60;
    }

    total_minutes = hours * 60 + minutes; // now we know how many minutes are in the interval
    return Math.floor(total_minutes / 30); // or ceil or round or whatever you want
}

Also, don't name your functions 'process_X' as it doesn't really describe the function.


The other answers work but you'll run into problems if you're trying to find the number of 30 minute blocks that occur between times on different dates. It's better if you convert your startTime and endTime to Date objects and compare then number of 30 minute blocks between each.

The following accepts two dates and returns the number of full 30 minute blocks between the two dates. In this example, I've added a random 654 minutes but you can modify however you like:

var now = new Date();
var later = new Date(now);
later.setMinutes(now.getMinutes() + 654);

function process_blocks(startTime,endTime) {
    if (endTime < startTime) {
        alert('endTime must be greater than startTime');
    }
    else {
        alert('There are ' + Math.round((endTime - startTime)/1000/60/30) + ' full 30 minute blocks between the two times' );
    }
}

process_blocks(now,later);


Convert everything to an amount of minutes, then divide by 30 for the result (which you can round if needed)

var start = Math.floor(startTime/100)*60 + (startTime%100);
var end = Math.floor(endTime/100)*60 + (endTime%100);
return (end-start)/30;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜