How to subtract one year from a Date type in Angular 2?
11,133
You could use Date#setFullYear
to set the year to one less than Date#getFullYear
:
const now = new Date();
now.setFullYear(now.getFullYear() - 1);
document.write(now.toISOString().slice(0,10));
Author by
Samuel Ribeiro
Updated on June 25, 2022Comments
-
Samuel Ribeiro almost 2 years
I want to subtract one year from a
Date
in my Angular component. This is my template:<input type="date" [value]="date" (change)="date=$event.target.value" class="form-control" />
And the associated class:
export class appResultadosUtenteComponent { date: string; constructor() { this.date = new Date().toISOString().slice(0, 10); } }