JS通过一个值与日期比较计算天数

JavaScript021

JS通过一个值与日期比较计算天数,第1张

<SCRIPT LANGUAGE="JavaScript">

<!--

function deal(){

//var Y = 6

//var R ="2009-11-01"

var R = document.getElementById('date').value +''

var Y = document.getElementById('month').value

alert(R)

var year=R.substr(0,4)

var month=R.substr(5,2)

var day=R.substr(8,2)

if((parseInt(month)+parseInt(Y)) >12){

year++

month = parseInt(month) + parseInt(Y) - 12

}else{

month = parseInt(month) + parseInt(Y)

}

alert(year+"-"+month+"-"+day)

R = new Date(year,month,day,00,00,00)

dateNow = new Date()

var diff = R.getTime() - dateNow.getTime()

var days = Math.floor(diff/(1000*60*60*24))

var str = "与今天相差" + days + "天"

if(R >dateNow){

alert("大于今天!"+"\n"+str)

}else{

alert("小于今天!"+"\n"+str)

}

}

//-->

</SCRIPT>

月<input type="text" id="month" value="6">

日期 <input type="text" id="date" value="2009-11-01">

<input type="button" value="submit" onClick="deal()">

这样的吗

<!DOCTYPE HTML>

<html lang="en-US">

<head>

<meta charset="UTF-8">

<meta name="keywords" content="白菜编辑部">

<title>白菜编辑部</title>

<style type="text/css">

</style>

<script type="text/javascript">

var test = function ()

    {

    var reg = /[^\d]/g

    var t1 = document.getElementById ('txt1')

    var t2 = document.getElementById ('txt2')

    var start = new Date (t1.value.replace (reg, '/'))

    var end = new Date (t2.value.replace (reg, '/'))

    var day = Math.floor (Math.abs (start.getTime () - end.getTime ()) / 1000 / 60 / 60 / 24)

    alert("之间的天数:" + day)

    }

</script>

</head>

<body>

<input type="text" value="2014-11-14" id="txt1" />

<input type="text" value="2014-12-01" id="txt2" />

<button onclick="test()">test</button>

</body>

</html>