我正在尝试使用角度4在屏幕上显示两年的值。第一个是当前年份,第二个是当前年份加7。请问我是否以正确的方式进行操作?
组件代码
currentYear = new Date();
currentYearPlusSix = new Date();
currentYearPlusSix1 = this.currentYearPlusSix.setDate(this.currentYearPlusSix.getFullYear() + 7 );
html
The current year is {{currentYear | date:'yyyy'}}
The current year plus six is {{currentYearPlusSix1 | date:'yyyy'}}
最佳答案
使用setFullYear
设置年份,而不是setDate
。setDate
显然是错误的用法,并且会给出错误的结果。
currentYearPlusSix1 = this.currentYearPlusSix.setFullYear(this.currentYearPlusSix.getFullYear() + 7 );
setDate
接受dayValue
-代表月份中的一天的整数。如果过了一年,您会得到不同的结果currentYear = new Date();
currentYearPlusSix = new Date();
currentYearPlusSix1 = this.currentYearPlusSix.setDate(this.currentYearPlusSix.getFullYear() + 7);
console.log(currentYearPlusSix)
改用
setFullYear
更改年份currentYear = new Date();
currentYearPlusSix = new Date();
currentYearPlusSix1 = this.currentYearPlusSix.setFullYear(this.currentYearPlusSix.getFullYear() + 7);
console.log(currentYearPlusSix)
关于angular - 使用 Angular 4显示年份,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48520586/