使用javascript计算两个日期之间的天数

时间:2012-07-06 10:16:15

标签: javascript html

<link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.min.css" />
<script type="text/javascript" src="jsDatePick.min.1.3.js"> </script>
<script type="text/javascript">
    window.onload = function(){
        var fromObj,toObj;
        fromObj=new JsDatePick({
            useMode:2,  target:"inputField1",dateFormat:"%d-%M-%Y"

        });     
        toObj=new JsDatePick({
            useMode:2,
            target:"inputField2",
            dateFormat:"%d-%M-%Y"

        });             

function viewAlert(){
    //what i need to put it here?   

}

    };
</script>
</head>
<body>
<form name="form1">

<table align="center">
    <tr><td>from:</td><td><input type="text" size="12" id="inputField1" name="from" /></td></tr>
    <tr><td>to:</td><td><input type="text" size="12" id="inputField2" name="to" onblur="viewAlert()" /></td></tr>
    </table>

    </form>

我创建了两个输入字段,从jsDatePicker获取日期。输入这些日期后,我需要在警告框中显示所选日期的天数。为此我需要做什么?

提前致谢

1 个答案:

答案 0 :(得分:0)

var msDay = 24*60*60*1000; 
var days = Math.round(Math.abs((fromObj.getTime() - toObj.getTime())/(msDay )));