我在向日期对象添加1年时遇到问题(从日期开始),并将其设置为endDate to my to datepicker。它添加所有完美除了几个月少于2.这是我用来添加1年的代码。
var fromMaximumDate = new Date(Date.parse($('#dtFromDate').val()));
fromMaximumDate.setFullYear(fromMaximumDate.getFullYear() + 1);
fromMaximumDate.setMonth(fromMaximumDate.getMonth());
fromMaximumDate.setDate(fromMaximumDate.getDate());
fromMaximumDate.setHours(fromMaximumDate.getHours());
fromMaximumDate.setMinutes(fromMaximumDate.getMinutes());
fromMaximumDate.setSeconds(fromMaximumDate.getSeconds());.
下图:
答案 0 :(得分:1)
请尝试以下代码:
var fromMaximumDate = new Date(Date.parse($('#dtFromDate').val()));
alert(fromMaximumDate.getMonth());
如果您看到添加了2的月份,那么您就知道您的Date.parse()
代码是罪魁祸首。
答案 1 :(得分:1)
Your code looks right to me. Have a look to this fiddle link.
https://jsfiddle.net/dscfzwx8/
Just remember that int ISO notation 2015 is the year, 05 is the month and 07 is the day