get the date 5 years ago in typescript

To get the date 5 years ago in TypeScript, you can use the Date object and the setFullYear method. Here's an example:

index.ts
const today = new Date();
const fiveYearsAgo = new Date();
fiveYearsAgo.setFullYear(today.getFullYear() - 5);
110 chars
4 lines

In this code, we create a today variable that holds the current date. We then create a new Date object and set it to hold the same date as today. Finally, we use the setFullYear method to subtract 5 years from the year of our new Date object, giving us the date from 5 years ago.

Alternatively, you can also use the getTime and setTime methods to accomplish the same task:

index.ts
const today = new Date();
const fiveYearsAgo = new Date(today.getTime());
fiveYearsAgo.setTime(fiveYearsAgo.getTime() - (5 * 365 * 24 * 60 * 60 * 1000));
154 chars
4 lines

In this code, we again create a today variable that holds the current date. We then create a new Date object and set it to hold the same date as today. Finally, we use the getTime and setTime methods to subtract 5 years in milliseconds from the time of our new Date object.

gistlibby LogSnag