[英]how to increment date by giving the number of days in javascript
I want to increment dates using JavaScript I used .setDate(1)
to increment dates by one day but if the date is 31/11/2011
after increment becomes 1/0/2012
, 我想增加使用JavaScript我使用日期
.setDate(1)
由一个一天,但如果日期是递增日期31/11/2011
增量之后变为1/0/2012
,
the question is how to increment date by giving the number of days . 问题是如何通过给出天数来增加日期。
js js
newDate.setDate(newDate.getDate()+1);
alert(newDate.getFullYear()+"-"+newDate.getMonth()+"-"+newDate.getDate());
That is correct, because in javascript, months are indexed from 0, not 1. 没错,因为在javascript中,月份是从0而不是1索引的。
You need to alert like this instead: 您需要这样提醒:
alert(newDate.getFullYear()+"-"+(newDate.getMonth()+1)+"-"+newDate.getDate());
鉴于Javascript中的月份范围为0到11,这是没有错的。因此,当您说到31/11/2011
,javascript可以理解的是31/12/2011
。
Lets make it some more clear: 让我们更清楚一点:
var Date = new Date();
var DaysToAdd = 6;
someDate.setDate(Date.getDate() + DaysToAdd);
Formatting Date to dd/mm/yyyy format: 将日期格式化为dd / mm / yyyy格式:
var dd = Date.getDate();
var mm = Date.getMonth() + 1;
var yyyy = Date.getFullYear();
var NewDate = dd + '/'+ mm + '/'+ yyyy;
Hope this helps. 希望这可以帮助。
You can use like this, Suppose you want to increment current date by 2 days then, 您可以这样使用,假设您想将当前日期增加2天,
var today = new Date(); // Or Date.today()
var newDate = today.add(2).day();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.