How To Get The Current Year In Typescript

Knowing how to get the current year in Typescript will be very helpful for you when you are working with the user interface. So how to do it? Let’s go into detail now.

Get the current Year in TypeScript

Use the getFullYear method

The getFullYear() method will return four digits representing the full year of a date.

Syntax:

Date.getFullYear()

Example:

console.log(new Date("2022-10-25").getFullYear())

Output:

[LOG]: 2022

As you see, I use the getFullYear method with the 2022-10-25 date, which day is 25, the month is 10, and the year is 2022, so the value return will be 2022. To apply the getFullYear method to get the current year in Typescript, you will have to get the correct current time with the Date constructor.

Example:

console.log(new Date())

Output:

[LOG]: Date: "2022-10-17T00:46:35.456Z"

As you see here, if I initialize a new Date constructor without put in any time, I can get the current time in full-text string format. And I can combine it with the getFullYear method to get the current year.

Example:

const currTime = new Date()
console.log(currTime)
console.log(currTime.getFullYear())

Output:

[LOG]: Date: "2022-10-17T00:48:47.688Z" 
[LOG]: 2022

Some useful method 

Besides getFullYear() method to get a year, you can also have some use the full method to work with a year, like getMonth:

Example:

const currTime = new Date()
console.log(currTime)
console.log('current month: '+currTime.getMonth())

Output:

[LOG]: Date: "2022-10-17T00:52:04.161Z" 
[LOG]: "current month: 9"

Notice that on the current date, the month is 10, but with the getMonth method, the current month is 9. It’s pretty weird, right? But the reason is because Typescript returns values in 0 bases, which means January will return 0. So You always must plus one to getMonth method value.

Example:

const currTime = new Date()
console.log(currTime)
const currentMonth = currTime.getMonth() +1
console.log('current month: '+currentMonth)

Output:

[LOG]: Date: "2022-10-17T00:55:04.425Z" 
[LOG]: "current month: 10"

You can also get the current date by the getDate() method.

Example:

const currTime = new Date()
console.log(currTime)
console.log('current date: '+currTime.getDate())

Output:

[LOG]: Date: "2022-10-17T00:55:04.425Z" 
[LOG]: "current month: 10" 
[LOG]: Date: "2022-10-17T00:55:55.848Z" 
[LOG]: "current date: 17"

Or, if you want to format the date to a string base, you can use the toDateString method:

Example:

const currTime = new Date()
console.log(currTime)
console.log(currTime.toDateString())

Output:

[LOG]: Date: "2022-10-17T01:00:11.888Z" 
[LOG]: "Mon Oct 17 2022"

Summary

In this article, I showed and explained how to get the current year in Typescript. You can create a new Date constructor without passing any parameter to get the current time and using the getFullYear method to get the current year. I hope it is helpful to you.

Leave a Reply

Your email address will not be published. Required fields are marked *