Extract Year From Date in TypeScript

Have you ever worked on a feature where you needed to group records by year? I recently faced this while building an internal dashboard using TypeScript for tracking employee onboarding.

I was working on an employee management module built with TypeScript, where each employee’s joining date was stored in the system. My task was to display a report showing how many employees joined each year. To achieve this, I had to extract the year from the date values.

In the examples below, I will show how to extract year from date in TypeScript using the Date object.

Understanding Date Object in TypeScript

In TypeScript, the built-in Date object represents a single moment in time. It provides methods to work with dates and times, including retrieving and manipulating various components of a date. To extract the year from a date, we’ll primarily use the getFullYear() method.

The getFullYear() method returns the year of the specified date according to the local time zone. It returns a four-digit number representing the year.

Here’s a simple example to demonstrate the usage of getFullYear():

const currentDate = new Date();
const currentYear = currentDate.getFullYear();
console.log(currentYear); // Output: 2025 (assuming the current year is 2025)

Output:

Extract year from date in TypeScript

In this example, we create a new Date object representing the current date and time. We then call the getFullYear() method on the currentDate object to retrieve the current year and store it in the currentYear variable.

Check out: Get the Day of the Month in TypeScript

Extract Year From Specific Date in TypeScript

Example-1:

In real-world scenarios, you might have a specific date from which you need to extract the year. Let’s consider an example where we have a date of birth stored as a timestamp and we want to calculate the age of a person.

const dateOfBirth = new Date("1990-05-15");
const birthYear = dateOfBirth.getFullYear();
const currentYear = new Date().getFullYear();
const age = currentYear - birthYear;
console.log(`John Doe is ${age} years old.`);

Output:

Get year from date in Typescript

In this example, we create a Date object representing John Doe’s date of birth. We extract the birth year using getFullYear() and store it in the birthYear variable. We also retrieve the current year using getFullYear() on a new Date object.

To calculate John Doe’s age, we subtract birthYear from the currentYear. This gives us the age of the person, which we can then display or use for further processing.

Example-2:

In this example, we will extract the year from a specific date. In the intro, we had a scenario to extract the year from the employee’s joining date. Let’s add the employee’s joining date and see how we can extract the year using getFullYear() in the Date object.

const joiningDate = new Date("2025-02-15");
const joiningYear = joiningDate.getFullYear();
console.log(`Employee joined in the year: ${joiningYear}`);

Output:

Extract year from specific date in TypeScript

Check out: Get the Current Date in Milliseconds Using TypeScript

Parse Timestamps to Extract Year in TypeScript

Sometimes, you might have a timestamp in milliseconds or seconds that you need to convert to a Date object before extracting the year. TypeScript provides methods to parse timestamps and create Date objects accordingly.

Here’s an example that demonstrates parsing a timestamp in milliseconds:

const timestampInMilliseconds = 1721152000000; // May 16, 2025
const yeardate = new Date(timestampInMilliseconds);
const year = yeardate.getFullYear();
console.log(year); // Output: 2025

Output:

TypeScript get year from date

In this example, we have a timestamp in milliseconds representing May 16, 2025. We create a new Date object by passing the timestampInMilliseconds to the Date constructor. This converts the timestamp to a Date object.

We then call the getFullYear() method on the date object to extract the year, which is stored in the year variable. In this case, the extracted year would be 2025.

Handle Different Date Formats in TypeScript

In some cases, you might encounter dates in different formats, such as ISO 8601 or custom formats. TypeScript provides flexibility in parsing various date formats using the Date constructor.

Here’s an example that demonstrates parsing an ISO 8601 date string:

const isoDateString = "2024-12-31T23:59:59Z";
const date = new Date(isoDateString);
const year = date.getFullYear();
console.log(year); // Output: 2024

Output:

get year from date using date object in Typescript

In this example, we have an ISO 8601 date string representing December 31, 2024, at 23:59:59 UTC. We create a new Date object by passing the isoDateString to the Date constructor. TypeScript automatically parses the ISO 8601 format and creates a valid Date object.

We then extract the year using the getFullYear() method, which gives us the year 2024.

Check out: Format Dates in TypeScript

Calculate Years Between Dates in TypeScript

In certain scenarios, you might need to calculate the number of years between two dates. This can be achieved by comparing the years of both dates.

Here’s an example that demonstrates calculating the years between two dates:

const startDate = new Date("2018-06-15");
const endDate = new Date("2024-11-17");
const startYear = startDate.getFullYear();
const endYear = endDate.getFullYear();
const yearsDifference = endYear - startYear;
console.log(`The difference between the dates is ${yearsDifference} years.`);

Output:

Calculate Years Between Dates Typescript

In this example, we have a startDate representating June 15, 2018, and a endDate representation of November 17, 2025. We extract the years from both dates using the getFullYear() method and store them in the startYear, and endYear variables, respectively.

To calculate the years between the dates, we subtract the startYear from the endYear. This gives us the difference in years, which is stored in the yearsDifference variable. In this case, the difference would be 7 years.

Check out: Subtract Dates in TypeScript

Format Dates With Year in TypeScript

When presenting dates to users, it’s often necessary to format them in a specific way, including the year. TypeScript provides methods to format dates according to different locales and conventions.

Here’s an example that demonstrates formatting a date with the year:

const date = new Date("2025-03-16");
const formattedDate = date.toLocaleDateString("en-US", {
  year: "numeric",
  month: "long",
  day: "numeric"
});
console.log(formattedDate); // Output: March 16, 2025

Output:

Format Dates with the Year in TypeScript

In this example, we have a date object representing March 16, 2025. We use the toLocaleDateString() method to format the date according to the “en-US” locale. We specify the desired format by passing an options object with the year, month, and day properties set to “numeric” and “long”, respectively.

The formatted date string, including the year, is stored in the formattedDate variable. In this case, the output would be “March 16, 2025”.

Conclusion

In this TypeScript tutorial, we explored various scenarios where extracting the year is useful, such as calculating ages, parsing timestamps, handling different date formats, calculating years between dates, and formatting dates with the year.

I hope that with the help of the above examples, you understand how to use the Date object and its getFullYear() method to extract the year from a Date in TypeScript.

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.