Get the current Year using TypeScript

avatar

Borislav Hadzhiev

Last updated: Feb 17, 2022

banner

Photo from Unsplash

Get the current Year using TypeScript #

To get the current year in TypeScript:

  1. Call the new Date() constructor to get a date object for the current date.
  2. Call the getFullYear() method on the date object.
  3. The getFullYear method will return a number that represents the current year.
index.ts
// 👇️ const currentYear: number const currentYear = new Date().getFullYear(); console.log(currentYear); // 👉️ 2022

We used the Date() constructor to get a Date object that represents the current date.

index.ts
// 👇️ const now: Date const now = new Date(); console.log(now); // 👉️ Thu Feb 17 2022 11:27:54 GMT
The type of the now variable is correctly inferred to be Date, which enables us to use any of the built-in methods the Date object implements.

We called the Date.getFullYear method on the Date object to get a number that represents the current year.

Other commonly used methods on the Date object are:

  • Date.getMonth - returns an integer between 0 (January) and 11 (December) and represents the month for a given date. Yes, unfortunately the getMonth method is off by 1.
  • Date.getDate - returns the day of the month for a specific date

When working with Dates in TypeScript, you should always keep in mind that the getMonth() method returns a zero-based value, where January = 0, February = 1, etc, up to December, which is = 11.

index.ts
/** * 👉️ Today is 17th of February 2022 */ const now = new Date(); // 👇️ const currentYear: number const currentYear = now.getFullYear(); console.log(currentYear); // 👉️ 2022 // 👇️ const currentMonth: number const currentMonth = now.getMonth(); console.log(currentMonth); // 👉️ 1 (1 = February) // 👇️ const currentDayOfMonth: number const currentDayOfMonth = now.getDate(); console.log(currentDayOfMonth); // 👉️ 17

The output for the getMonth method shows 1, which is February, because the method returns a zero-based value.

You will often see developers add 1 to the result when formatting the output to a visitor's browser.

It should be noted that you can use the getFullYear() method to get the year of any valid Date object, now just the current date.

index.ts
// 👇️ const date: Date const date = new Date('2023-09-24'); // 👇️ const currentYear: number const currentYear = date.getFullYear(); console.log(currentYear); // 👉️ 2023 // 👇️ const currentMonth: number const currentMonth = date.getMonth(); console.log(currentMonth); // 👉️ 8 (8 = September) // 👇️ const currentDayOfMonth: number const currentDayOfMonth = date.getDate(); console.log(currentDayOfMonth); // 👉️ 24

In this example, we create a date for the 24th of September, 2023 and use all of the aforementioned methods to get the year, month and day of the month of the date.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.