find the current year in typescript

In TypeScript, you can find the current year by creating a new Date object and calling the getFullYear() method on it, which returns the year of the specified date according to local time, as a four-digit number.

Here's how you can find the current year in TypeScript:

index.ts
const today: Date = new Date();
const currentYear: number = today.getFullYear();

console.log(currentYear); // outputs the current year
136 chars
5 lines

In the above code, we first create a new Date object today by calling the Date constructor with no arguments, which creates a new Date object set to the current date and time. We then call the getFullYear() method on today to retrieve the current year and store it in the currentYear variable. Finally, we log the value of currentYear to the console.

This should output the current year as a four-digit number, such as 2022.

gistlibby LogSnag