JS日期计算,加天数影响月份的解决方案
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="../Scripts/Datepicker/WdatePicker.js" type="text/javascript"></script>
<script type="text/javascript">
$(window).load(function () {
var da = new Date();
$("#uarrive").val(da.getFullYear()+"-"+(da.getMonth()+1)+"-"+da.getDate());
});
function getday() {
var d1 = $("#uarrive").val();
d1 = d1.split("-");
var d2 = $("#away").val();
d2 = d2.split("-");
var s = new Date();
s.setFullYear(d1[0], d1[1], d1[2]);
var a = new Date();
a.setFullYear(d2[0], d2[1], d2[2]);
var r = new Date(a - s);
var rs = r.getDate() + 1; d1[1]
alert(rs);
$("#day").val(rs);
}
function setday() {
var d1 = $("#uarrive").val();
d1 = d1.split("-");
var s = new Date(d1[0], d1[1], 0);
var getdays=s.getDate();//选中月的天数
var da = $("#day").val();
var day = Number(d1[2]) + Number(da);
if (day > getdays) {
var m = d1[1] + 1;
var d = day - getdays;
var setd = d1[0] + "-" + m + "-" + (d-1);
$("#away").val(setd);
} else {
var setd = d1[0] + "-" + d1[2]+ "-" + (day-1);
$("#away").val(setd)
}
}
</script>
</head>
<body>
<div>
<table>
<tr>
<td>到店:<input id="uarrive" type="text" runat="server" class="Wdate" onfocus="WdatePicker({skin:'whyGreen',dateFmt:'yyyy-M-dd'})" /></td>
<td>天数:<input id="day" type="text" onkeyup="setday()" runat="server" style=" width:10%;"/> 离店:<input
id="away" type="text" onchange="getday()" runat="server" style=" width:40%;" class="Wdate" onfocus="WdatePicker({skin:'whyGreen',dateFmt:'yyyy-M-dd'})"/></td>
<tr><td colspan="2">备注:<input id="remark" type="text" runat="server" /></td></tr>
</table>
</div>
<div class="left">
这是左边
</div>
<div class="right" >
这是右边
</div>
</body>
</html>
<head>
<title></title>
<script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="../Scripts/Datepicker/WdatePicker.js" type="text/javascript"></script>
<script type="text/javascript">
$(window).load(function () {
var da = new Date();
$("#uarrive").val(da.getFullYear()+"-"+(da.getMonth()+1)+"-"+da.getDate());
});
function getday() {
var d1 = $("#uarrive").val();
d1 = d1.split("-");
var d2 = $("#away").val();
d2 = d2.split("-");
var s = new Date();
s.setFullYear(d1[0], d1[1], d1[2]);
var a = new Date();
a.setFullYear(d2[0], d2[1], d2[2]);
var r = new Date(a - s);
var rs = r.getDate() + 1; d1[1]
alert(rs);
$("#day").val(rs);
}
function setday() {
var d1 = $("#uarrive").val();
d1 = d1.split("-");
var s = new Date(d1[0], d1[1], 0);
var getdays=s.getDate();//选中月的天数
var da = $("#day").val();
var day = Number(d1[2]) + Number(da);
if (day > getdays) {
var m = d1[1] + 1;
var d = day - getdays;
var setd = d1[0] + "-" + m + "-" + (d-1);
$("#away").val(setd);
} else {
var setd = d1[0] + "-" + d1[2]+ "-" + (day-1);
$("#away").val(setd)
}
}
</script>
</head>
<body>
<div>
<table>
<tr>
<td>到店:<input id="uarrive" type="text" runat="server" class="Wdate" onfocus="WdatePicker({skin:'whyGreen',dateFmt:'yyyy-M-dd'})" /></td>
<td>天数:<input id="day" type="text" onkeyup="setday()" runat="server" style=" width:10%;"/> 离店:<input
id="away" type="text" onchange="getday()" runat="server" style=" width:40%;" class="Wdate" onfocus="WdatePicker({skin:'whyGreen',dateFmt:'yyyy-M-dd'})"/></td>
<tr><td colspan="2">备注:<input id="remark" type="text" runat="server" /></td></tr>
</table>
</div>
<div class="left">
这是左边
</div>
<div class="right" >
这是右边
</div>
</body>
</html>