javascript日期添加错误天数

时间:2017-05-29 21:02:13

标签: javascript date

我在页面上使用此代码来显示接下来的几个日期,它们总是在接下来的几天......但它现在不能正常工作:

<script>var now = new Date();
    var day = ("0" + (now.getDate()+3)).slice(-2);
    var day2 = ("0" + (now.getDate()+4)).slice(-2);
    var month = ("0" + (now.getMonth() + 1)).slice(-2);
    var today = (month)+"/"+(day)+"/"+now.getFullYear();
    var today2 = (month)+"/"+(day2)+"/"+now.getFullYear();
    document.write(today); document.write(' and/or ');document.write(today2);
</script>

但它正在解决这个问题:

currently scheduled for: 05/32/2017 and/or 05/33/2017

如果需要,如何让+ 1让它到下个月?

1 个答案:

答案 0 :(得分:0)

试试这个

 <script>
var now = new Date();
    var day = ("0" + (now.setDate(now.getDate()+3)).getDate()).slice(-2);
    var day2 = ("0" + (now.setDate(now.getDate()+4)).getDate()).slice(-2);
    var month = ("0" + (now.setMonth(now.getMonth() + 1+1)).getMonth()).slice(-2);
    var today = (month)+"/"+(day)+"/"+now.getFullYear();
    var today2 = (month)+"/"+(day2)+"/"+now.getFullYear();
    document.write(today); document.write(' and/or ');document.write(today2);
</script>