How to Create a Date from Year, Month, and Day in TypeScript

In this tutorial, I will explain how to create a date object from a year, month, and day in TypeScript. If you have separate values for the year, month, and day and need to combine them into a valid date object, there are several ways to accomplish this using the built-in Date constructor, Date.UTC(), or Date.parse() methods in TypeScript.

Using the Date Constructor

One of the best ways to create a date from year, month, and day values is by using the Date constructor in TypeScript. The Date constructor accepts parameters for year, month (0-indexed), day, hour, minute, second, and millisecond.

Here’s an example of creating a date for April 27, 2025:

const year = 2025;
const month = 3; // April is 0-indexed month 3
const day = 27;

const today = new Date(year, month, day);
console.log(today); // Output: 2025-04-27T00:00:00.000Z 

In this code, we provide the year, month (as a 0-indexed value where 0 represents January), and day to the Date constructor. It returns a new Date object set to April 27, 2025.

Here is the exact output in the screenshot below:

create a date object from year month day in TypeScript

Check out Add Months to a Date in TypeScript

Using Date.UTC()

Another way to create a date from component values is by using the Date.UTC() method. It works similarly to the Date constructor but treats the provided values as UTC (Coordinated Universal Time).

const year = 2025;
const month = 3; // April is 0-indexed month 3
const day = 27;

const today = new Date(Date.UTC(year, month, day));
console.log(today); // Output: 2025-04-27T00:00:00.000Z

The Date.UTC() method returns the number of milliseconds since January 1, 1970 for the specified UTC date and time. We pass that value to the Date constructor to create a new Date object.

combine year month and day into a date in TypeScript

Check out Get the Current Date in TypeScript

Handling Time Zones

When using Date.UTC(), keep in mind that it interprets the provided values as UTC. If you want to create a date in a specific time zone, you can adjust the values accordingly or use the Date constructor with the desired local values.

For example, to create a date for April 27, 2025, at 6:00 PM in the Eastern Time Zone (EST):

const year = 2025;
const month = 3; // April is 0-indexed month 3
const day = 27;
const hour = 18;

const todayEvening = new Date(year, month, day, hour);
console.log(todayEvening); // Output: 2025-04-27T18:00:00.000Z (local time) or adjusted based on timezone

The output represents the equivalent UTC time, but the Date object is set to April 27, 2025, at 6:00 PM EST.

Parsing Date Strings

In some cases, you might have the year, month, and day as string values. To create a date from string values, you can use the Date.parse() method.

const dateString = '2025-04-27';

const today = new Date(Date.parse(dateString));
console.log(today); // Output: 2025-04-27T00:00:00.000Z

Date.parse() takes a string representation of a date and returns the number of milliseconds since January 1, 1970. We pass that value to the Date constructor to create a new Date object.

Check out: ISO Date Strings in TypeScript

Formatting Date Strings

When parsing date strings, it’s important to ensure that the string format is valid and recognized by Date.parse(). The supported formats include:

  • ISO 8601 format: ‘YYYY-MM-DD’ or ‘YYYY-MM-DDTHH:mm:ss.sssZ’
  • Short Date format: ‘MM/DD/YYYY’ or ‘YYYY/MM/DD’
  • Long Date format: ‘Month DD, YYYY’

Here are a few examples:

const isoFormat = '2025-04-27';
const shortFormat = '04/27/2025';
const longFormat = 'April 27, 2025';

const todayISO = new Date(Date.parse(isoFormat));
const todayShort = new Date(Date.parse(shortFormat));
const todayLong = new Date(Date.parse(longFormat));

console.log(todayISO); // Output: 2025-04-27T00:00:00.000Z
console.log(todayShort); // Output: 2025-04-27T00:00:00.000Z
console.log(todayLong); // Output: 2025-04-27T00:00:00.000Z

All three date string formats are parsed correctly and create equivalent Date objects.

Here is the exact output in the screenshot below:

TypeScript tutorial create date from individual year month day

Check out How to Set a Date Value to Null in TypeScript

Formatting and Displaying Dates

Once you have a Date object created from year, month, and day values, you can format and display it in various ways using TypeScript’s built-in methods.

Formatting with toLocaleDateString()

The toLocaleDateString() method allows you to format the date according to the specified locale and options.

const today = new Date(2025, 3, 27); // April is month 3 (0-indexed)

const formattedDate = today.toLocaleDateString('en-US', {
  year: 'numeric',
  month: 'long',
  day: 'numeric'
});

console.log(formattedDate); // Output: April 27, 2025

In this example, we format the date using the ‘en-US’ locale and specify the desired format options, such as displaying the year as a numeric value, the month as a long string, and the day as a numeric value.

Extracting Date Components

TypeScript provides methods to extract individual components from a Date object, such as the year, month, and day.

const today = new Date(2025, 3, 27); // April is month 3 (0-indexed)

const year = today.getFullYear();
const month = today.getMonth();
const day = today.getDate();

console.log(year); // Output: 2025
console.log(month); // Output: 3
console.log(day); // Output: 27

The getFullYear() method returns the year, getMonth() returns the month (0-indexed), and getDate() returns the day of the month.

Check out: Calculate Yesterday’s Date in TypeScript

Conclusion

In this tutorial, we explored various ways to create a date from year, month, and day values in TypeScript. Whether you have separate numeric values or string representations of the date, you can use the Date constructor, Date.UTC(), or Date.parse() methods to create a valid Date object.

We also looked at how to format and display dates using the toLocaleDateString() method and how to extract individual date components using methods like getFullYear(), getMonth(), and getDate().

In this tutorial, I have explained how to create a date from year, month and day values 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.