如何使用angular6中的typescript获取当前年份

如何使用angular6中的typescript获取当前年份,angular,typescript,angular6,Angular,Typescript,Angular6,如何使用angular6中的typescript获取当前年份 currentYear:Date; this.currentYear=new Date("YYYY"); alert(this.currentYear); 它显示无效日期 试试看 警报((新日期()).getFullYear())在javascript中,我们可以使用getFullYear()从日期对象获取年份 有关导入部分的所有日期对象,请参阅本页 import * as moment from 'moment' 在ngOnI

如何使用angular6中的typescript获取当前年份

currentYear:Date;
this.currentYear=new Date("YYYY");
alert(this.currentYear);
它显示无效日期

试试看


警报((新日期()).getFullYear())在javascript中,我们可以使用
getFullYear()
从日期对象获取年份

有关导入部分的所有日期对象,请参阅本页

import * as moment from 'moment'
在ngOnInit

ngOnInit(){
this.date = moment(new Date()).format('YYYY');
console.log(moment(new Date()).format('YYYY'));
}
您可以尝试以下方法:

在app.component.ts中

export class AppComponent  {
  anio: number = new Date().getFullYear();
}
在app.component.html中

{{ anio }}
我给你一个闪电战

使用力矩库:

import * as moment from 'moment'

moment().year(); // current year
当然,您也可以使用以下代码:

moment().format('YYYY'); // current year

试试这个,因为年份是数字

 currentYear: number=new Date().getFullYear();

你自己设置年份,然后再检索。这太过分了
 currentYear: number=new Date().getFullYear();