使用{{currentDate | date:'MMM'}}
我们得到当前月份。但是,我们如何用这个逻辑来获得接下来的两个月呢?
我尝试使用"MMM+1"和"+1 MMM",但没有运气。
尝试使用这样的管道作为日期:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'addMonth'
})
export class AddMonthPipe implements PipeTransform {
transform(value: Date | number): number {
const date = new Date(value);
return date.setMonth(date.getMonth() + 1);
}
}
在 HTML 中:
{{currentDate | addMonth | date:'MMM'}}
在 HTML 中添加 : -
当月:{{currentDate | date:'MMM'}} <br>
下个月:{{addMonthInCurrentDateValue(1) | date:'MMM'}} <br>
下个月:
{{addMonthInCurrentDateValue(2) | date:'MMM'}} <br>
在 .ts 文件中添加以下代码。
addMonthInCurrentDateValue(n) {
let endDate: Date;
let today = new Date();
const nmonth = today.getMonth() + n;
var nextMonthDate = new Date(today.setMonth(today.getMonth() + n));
endDate = new Date(today.getFullYear(), nmonth, 0);
return today;
}