Typescript Date | Typescript new date

In TypeScript, the Date type is a powerful tool for managing dates and times. The Date type in TypeScript is an object that represents a moment in time, typically expressed as a date and time of day. Here, we will go through the Typescript date and also the new date in Typescript.

Typescript Date

You can use the Date object to work with date manipulation in TypeScript. This built-in object to do all date and time-related things. To initialize a new Date object, you can either use the constructor without arguments to capture the current date and time or pass specific arguments to tailor the date to your requirements.

Here is the complete code:

// Current date and time
const dateNow = new Date();

// Specific date: Nov 10, 2023
const dateCustom = new Date(2023, 10, 10);

Once you run the code using Visual Studio code, you can see the output in the screenshot below.

typescript date

Once you have your Date object in Typescript, you can access and modify its properties. TypeScript provides a set of methods to retrieve and adjust the year, month, day, and other components.

Here is an example.

const date = new Date();
// Get year and month
const year = date.getFullYear();
const month = date.getMonth();
console.log(`Current Year: ${year}, Current Month: ${month}`);

// Set year and month

You can see the output in the screenshot below.

date type in typescript

Typescript new date

The new Date() constructor is your gateway to creating date objects representing a single moment in time in TypeScript. These date objects are based on a time value that is the number of milliseconds since January 1, 1970, UTC.

Here is how to instantiate a new date object in Typescript.

let currentDate = new Date();
console.log(currentDate); // Outputs the current date and time

The above Typescript code prints the current date and time to the console.

In Typescript, you can also create a date object for a specific date and time like the below code:

let specificDate = new Date('2023-01-01T00:00:00');

And, if you want to construct a date from individual components like year, month, and day, you can write the below Typescript code:

let dateFromComponents = new Date(2023, 0, 1); // Note: Month is 0-indexed

Typescript also provides various methods for getting and setting the various components of a date.

Here are some of the get method examples in Typescript date.

let date = new Date();

// Get full year
let year = date.getFullYear();
console.log(year); // Outputs the current year

// Get month (0-indexed)
let month = date.getMonth();
console.log(month); // Outputs the current month - 1

// Get day of the month
let day = date.getDate();
console.log(day); // Outputs the current day of the month

In the same way, you can see there are some setting methods in Typescript date.

let date = new Date();

// Set full year
console.log(date); // Outputs the date set to the year 2024

// Set month (0-indexed)
console.log(date); // Outputs the date set to December of the current year

// Set day of the month
console.log(date); // Outputs the date set to the 25th of the current month


Now, I hope you understand how to work with the Typescript date object, and also understand the new Date() Typescript constructor and associated methods in TypeScript. TypeScript’s Date object is always helpful in working with the current date and time in Typescript.

You may like: