计算24小时选择框的时差

时间:2015-07-22 16:31:13

标签: javascript

我正在使用返回NaN的这个jscript函数遇到一些麻烦。我在inTimeoutTime上尝试了parseInt,但这对我都不起作用。我希望得到一些指导。

function diffHours (h1,m1,h2,m2,t) {
    var inTime = ((h1 * 60) + m1);
    var outTime =((h2 * 60)+ m2);


    /* Converts total in minutes to "hh:mm" format */
    function toText (m) {
        var minutes = m % 60;
        var hours = Math.floor(m / 60);

        minutes = (minutes < 10 ? '0' : '') + minutes;
        hours = (hours < 10 ? '0' : '') + hours;

        return hours + ':' + minutes;
    }

    h1 = parseInt(inTime, 10);
    h2 = parseInt(outTime, 10);

    var diff = h2 - h1;


    document.getElementById(t).value = toText(diff)
}

然后我在文档中有4个选择框:执行此函数onChange的小时,分​​钟,小时,分钟输出,并应将其输出到具有时间差异的只读输入。 编辑:这里是输入

<cfset timeHour = ['','00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23']>
<cfset timeMinute = ['','00','01','02','02','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','23','24','25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50','51','52','53','54','55','56','57','58','59']>

<td><cfselect name="sun_in_hh" id="sun_in_hh" onChange="diffHours('sun_in_hh','sun_in_mm','sun_out_hh','sun_out_mm','sun_total')">
            <cfloop array="#timeHour#" index="h">
                <option value="#h#">#h#</option>
            </cfloop>
        </cfselect>
        <cfselect name="sun_in_mm" id="sun_in_mm">
            <cfloop array="#timeMinute#" index="m">
                <option value="#m#">#m</option>                             
            </cfloop>
        </cfselect></td>
<td><cfselect name="sun_out_hh" id="sun_out_hh" onChange="diffHours('sun_in_hh','sun_in_mm','sun_out_hh','sun_out_mm','sun_total')">
            <cfloop array="#timeHour#" index="h">
                <option value="#h#">#h#</option>
            </cfloop>
        </cfselect>
        <cfselect name="sun_out_mm" id="sun_out_mm">
            <cfloop array="#timeMinute#" index="m">
                <option value="#m#">#m#</option>                                
            </cfloop>
        </cfselect></td>
<td><cfinput type="text" name="sun_total" id="sun_total" size=5></td>

1 个答案:

答案 0 :(得分:0)

所以很可能你的情况是,正如你所说他们来自选择框

diffHours("22","45","23","15") or

diffHours("2j2","45","23","15")

&#13;
&#13;
function diffHours (h1,m1,h2,m2,t) {
    var inTime = ((h1 * 60) + m1*1); // convert the strings to number 
    var outTime =((h2 * 60)+ m2*1);  // convert the strings to number 


    /* Converts total in minutes to "hh:mm" format */
    function toText (m) {
        var minutes = m % 60;
        var hours = Math.floor(m / 60);

        minutes = (minutes < 10 ? '0' : '') + minutes;
        hours = (hours < 10 ? '0' : '') + hours;

        return hours + ':' + minutes;
    }

    h1 = parseInt(inTime, 10);
    h2 = parseInt(outTime, 10);

    var diff = h2 - h1;


    alert(toText(diff))
}
diffHours(22,45,23,15)
diffHours("22","45","23","15")
diffHours("2j2","45","23","15")
&#13;
&#13;
&#13;