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();
console.log(date);

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

2022-07-13T05:54:07.270Z

Useful methods to represent the date in different ways:

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

toDateString():

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

toISOString():

It converts the date to a string in ISO 8601 format

toLocaleDateString():

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

toLocaleString():

Locality sensitive representation of the date object.

toLocaleTimeString():

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

toString():

String representation of the date object.

toUTCString:

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();

console.log(today);

It will print something like below:

2022-7-10

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);

console.log(today);

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.

2022-07-10

You might also like: