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));
Share:
11,133
Samuel Ribeiro
Author by

Samuel Ribeiro

Updated on June 25, 2022

Comments

  • Samuel Ribeiro
    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);
        }
    }