慕容森
2021-05-06 14:32:59
嗨,我想加上12個月,并減去當前日期的1天。例子 :valStartDate:2018-01-20預期日期:2019-01-19我嘗試下面的代碼,但出現錯誤“ getFullYear()不允許使用的函數”this.endDate =this.valStartDate.getFullYear()+1+'-'+this.valStartDate.getMonth()+'-'+(this.valStartDate.getDate()-1);
3 回答

幕布斯7119047
TA貢獻1794條經驗 獲得超8個贊
確保給定的開始日期是日期而不是字符串。
var startDate = new Date(2018, 0, 20);
var startDatePlus12Months = new Date(startDate.setMonth(startDate.getMonth() + 12));
var expectedDate = new Date(startDatePlus12Months.getFullYear(), startDatePlus12Months.getMonth(), startDatePlus12Months.getDate() - 1);

守候你守候我
TA貢獻1802條經驗 獲得超10個贊
this.endDate = new Date(this.endDate); // <= maybe you get a string date...
this.endDate.setMonth(this.endDate.getMonth() + 12);
this.endDate.setDate(this.endDate.getDate() - 1);
如果要從服務器或以前的Json格式獲取日期,則可能需要將其從轉換string為Datefirst :this.endDate = new Date(this.endDate);??磥磉@是您的情況。
添加回答
舉報
0/150
提交
取消