<input id="Text1" type="text" onpropertychange="" onfocus="javascript:calendar(document.getElementById('Text1'))" /> 有两个文本框  一个开始时间和结束时间..怎么在焦点离开后计算中间的天数并计算  单价*天数 我不知道怎么写JS.

解决方案 »

  1.   

    <script language="javascript" type="text/javascript">
                
                    function DateDiff()
                    { //sDate1和sDate2是年-月-日格式
                    var sDate1 = document.getElementById("startDT").value;
                    var sDate2 = document.getElementById("endDT").value;
                    var price=document.getElementById("price").value;
                    if(sDate1!="" && sDate2!=""){
                    var aDate,oDate1,oDate2,iDays;
                    aDate=sDate1.split("-");
                    oDate1=new Date(aDate[1]+'-'+aDate[2]+'-'+aDate[0]);//转换为月-日-年格式
                    aDate=sDate2.split("-");
                    oDate2=new Date(aDate[1] + '-'+aDate[2]+'-'+aDate[0]);
                    iDays=parseInt(Math.abs(oDate1-oDate2)/1000/60/60/24); //把相差的毫秒数转换为天数
                    alert(price*iDays);
                    }
                    }
                </script>
                单价:<input id="price" type="text"/>
                开始:<input id="startDT" type="text" onmouseout="DateDiff()" />
                结束:<input id="endDT" type="text" onmouseout="DateDiff()" />