Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 788
Revathi Vijay Avatar asked Aug 29 '18 05:08

Revathi Vijay


People also ask

How do you get the current year in TypeScript?

To get the current year in TypeScript:Call the new Date() constructor to get a date object for the current date. Call the getFullYear() method on the date object. The getFullYear method will return a number that represents the current year.

How do I get current month and year in TypeScript?

To get the current month in typescript has the new Date(). getMonth() method which will return every time the current month index in number datatype. we have to just call new Date(). getMonth() and it will check the current date and return month index based on the date.

How do I find the current year of date?

To get the current year, use the getFullYear() JavaScript method. JavaScript date getFullYear() method returns the year of the specified date according to local time. The value returned by getFullYear() is an absolute number.


2 Answers

Try,

 alert((new Date()).getFullYear());
like image 135
Sajeetharan Avatar answered Sep 29 '22 01:09

Sajeetharan


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

like image 44
Simon Botero Avatar answered Sep 29 '22 01:09

Simon Botero