How to get the current date in TypeScript

How to get the current date in TypeScript:

The Date object is used to get the current date in TypeScript. We can use it to get the current date-time and also change it to different formats.

Different types of Date() object constructor:

We can create a new Date object and each time we create a Date object, it returns the current date-time.

There are different ways to construct a Date object with its constructor.

1. new Date():

The constructor is not taking any parameters. It will create one Date object with current date and time.

2. new Date(ms):

We can pass the number of milliseconds since January 1, 1970, 00:00:00 UTC.

3. new Date(dateStr):

It takes a string that represents a date. It is advised to use ISO 8601 format strings YYYY-MM-DDTHH:mm:ss.sssZ*.*If the string includes the only date part, it is treated as UTC. If we also pass the time without a timezone, it will be considered local.

4. new Date(date):

If we pass a date object, it creates a copy of that date object with equivalent data and returns that object.

5. new Date(year, month, day, hour, min, seconds, ms)

We can pass the year, month, day, hour, minute, seconds, and milliseconds values to the constructor of Date to create a Date object. Here, year and month are required. Other values are optional. The value starts from 1 for day and it starts from 0 for all other parameters.

Get the current date in TypeScript by using the Date constructor:

Let’s use the Date constructor to get the current date:

let date = new Date();

It will print the date-time string as like below:


Useful methods to represent the date in different ways:

We can use the below methods to represent the current date-time in different ways:


It returns the date part of the object in human-readable format.


It converts the date to a string in ISO 8601 format


Locality sensitive representation of the date portion of the date object.


Locality sensitive representation of the date object.


Locality sensitive representation of the time portion of the date object.


String representation of the date object.


Date to a string using UTC timezone.

let date = new Date();

console.log(date.toDateString()); // Sun Jul 10 2022
console.log(date.toISOString()); // 2022-07-10T06:03:56.241Z
console.log(date.toLocaleDateString()); // 10/7/2022
console.log(date.toLocaleString()); // 10/7/2022, 11:33:56 am
console.log(date.toLocaleTimeString()); // 11:33:56 am
console.log(date.toString()); // Wed Jul 10 2022 11:33:56 GMT+0530 (India Standard Time)
console.log(date.toUTCString()); // Wed, 10 Jul 2022 06:03:56 GMT

Method 2: Get the year, month and day:

Date object also provides different getter methods. We can use these methods to create our own date string. For example:

let date = new Date();

let today = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();


It will print something like below:


Get current date yyyy-mm-dd

As shown in the above example, we can change the date string to any format we want. For example,

const padZero = (num: number, pad: number) => num.toString().padStart(pad, '0');

let date = new Date();

let today = date.getFullYear() + "-" + padZero((date.getMonth() + 1), 2) + "-" + padZero(date.getDate(), 2);


Here, we created one new function padZero that will add zeroes to the start of a number to convert it to a fixed length string.

It will create date of yyyy-mm-dd.


You might also like: