To get the date 5 years ago in TypeScript, you can use the Date
object and the setFullYear
method. Here's an example:
index.ts110 chars4 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.ts154 chars4 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