How to get the current year using typescript in angular6
currentYear:Date; this.currentYear=new Date("YYYY"); alert(this.currentYear);
It shows Invalid Date
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With