shriyansh jain
shriyansh jain

Reputation: 109

How to calculate old date from current date in typescript or angular4+

How can i get the 90 days old date from current date in typescript or angular4

javascript function :

new Date().setMonth(new Date().getMonth() - 6)

is not working in angular/typescript

Upvotes: 0

Views: 1200

Answers (2)

Seryoga
Seryoga

Reputation: 851

Here is an exmaple:

subtract-days.ts:

export function subtractDays(days: number, fromDate: Date = new Date()): Date {
    return new Date(fromDate.getTime() - (days * 24 * 60 * 60 * 1000));
}

And in your angular component import the function and:

your-component.component.ts:

yourOldDate: Date = subtractDays(90);

Upvotes: 3

shriyansh jain
shriyansh jain

Reputation: 109

This is reference for angular/typescript.

To subtract a date from current date time, first need to get the time and then it needs to subtract the time difference you want it.

Example : if you need 90 days difference from current date

currentDate:Date = new Date();
currentDateTime = this.currentDate.getTime();
date = this.currentDateTime - (90 * 24 * 60 * 60 * 1000);
oldDate:Date = new Date(this.date);

Upvotes: 1

Related Questions