我现在想不到......我一直试图弄清楚两个变量之间有多少半小时,这将有如此的开始和结束时间,1100和1430
我开始做以下事情,但意识到时间不到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;
}
答案 0 :(得分:0)
你需要对待成千上万的人。数百小时,数十小时和数十小时那些数字是分钟。
24小时总是有4位数,所以取前两个,并比较:
$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;
答案 1 :(得分:0)
在分钟中计算区间,然后除以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
}
另外,不要将函数命名为'process_X',因为它没有真正描述函数。
答案 2 :(得分:0)
将所有内容转换为分钟数,然后将结果除以30(如果需要,可以将其舍入)
var start = Math.floor(startTime/100)*60 + (startTime%100);
var end = Math.floor(endTime/100)*60 + (endTime%100);
return (end-start)/30;
答案 3 :(得分:0)
其他答案有效,但如果您试图找到不同日期之间发生的30分钟数量的块,则会遇到问题。如果将startTime和endTime转换为Date对象并比较每个对象之间的30分钟块数,则会更好。
以下内容接受两个日期并返回两个日期之间的完整30分钟块数。在这个例子中,我添加了一个随机的654分钟,但你可以修改你喜欢的:
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);
答案 4 :(得分:0)
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));
}