How to get the current year using typescript in angular6
currentYear:Date;
this.currentYear=new Date("YYYY");
alert(this.currentYear);
It shows Invalid Date
How to get the current year using typescript in angular6
currentYear:Date;
this.currentYear=new Date("YYYY");
alert(this.currentYear);
It shows Invalid Date
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.
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'));
}
This one is so easy.
In the .ts file use the following function.
currentYearLong(): number {
return new Date().getFullYear();
}
and now in the html use curly bracket to access the year number.
<footer class="deep-color white-text center">
<p class="flow-text">great company © {{currentYearLong()}}</p>
</footer>
In javascript, we can get the year from date object using getFullYear().
currentYear: Date;
ngOnInit() {
this.currentYear = new Date("2017");
console.log(this.currentYear.getFullYear());
}
Refer this page for all date object methods
use moment library:
import * as moment from 'moment'
moment().year(); // current year
of course you can use this code as well:
moment().format('YYYY'); // current year