How to get the current year using typescript in angular6

How to get the current year using typescript in angular6

currentYear:Date;
this.currentYear=new Date("YYYY");
alert(this.currentYear);

It shows Invalid Date


Try,

 alert((new Date()).getFullYear());

You can try this:

In the app.component.ts.

export class AppComponent  {
  anio: number = new Date().getFullYear();
}

In the app.component.html

{{ anio }}

I let you a Stackblitz.

https://stackblitz.com/edit/angular-byvzum


Since Year is number.

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

At the import section,

import * as moment from 'moment'

At the ngOnInit,

ngOnInit(){
this.date = moment(new Date()).format('YYYY');
console.log(moment(new Date()).format('YYYY'));
}

Try this👇

year = new Date().getFullYear()
console.log(this.year) // output 2020