How To Get The Current Date And Time In Typescript?

Get the current date and time in TypeScript

Knowing how to get the current date and time in Typescript will help you when you want to design the UI. You can use Date() constructor to do this task. So let’s check this post to see the specific steps!

Get the current date and time in Typescript

Use Date() constructor

The Date() constructor will help you return a string and show you the current time.

Example:

const currentDate = new Date();
console.log(currentDate);

Output:

2022-10-07T11:27:44.005Z

If you want to change the current time to format YYYY-MM-DD or you want to know more about how to get the year, month, and day in a separate way. I also have already written a post about it. You can read more about it here.

The ways to initialization Date

There are one more few ways to initialization Date:

Date(millisecond)

Date(year, month, day, hour, minute, second, millisecond)

Example:

const currentDate = new Date();
console.log(currentDate);

// Date(millisecond)
console.log(new Date(1000000000));

// Date(year, month, day, hour, minute, second, millisecond)
console.log(new Date(2030, 12, 1, 3, 23, 43, 23));

Output:

2022-10-07T11:39:36.348Z
1970-01-12T13:46:40.000Z
2030-12-31T20:23:43.023Z

The ways to format dates

Typescript also provides us with some methods to format dates.

Use toLocaleString

The toLocaleString() method returns a string show date according to your location.

Syntax:

toLocaleString(locales,option)

Parameters:

  • locales: Contain 47 language-specific standards, the form of how time should be shown.
  • option: An object containing some property that is set for time showing.

Example:

const currentDate = new Date();
console.log(currentDate);

// Use the toLocaleString() method
console.log(currentDate.toLocaleString());

Output:

2022-10-07T11:56:19.364Z
10/7/2022, 6:56:19 PM

Use toDateString()

The toDateString() method will help you return Date that represents the local timezone in English.

Syntax:

Date.toDateString()

Example:

const currentDate = new Date();
console.log(currentDate);

// Use the toDateString() method
console.log(currentDate.toDateString());

Output:

2022-10-07T12:00:29.576Z
Fri Oct 07 2022

Use toISOString()

The toISOString() method will help you return a string representing the Date in ISO format: YYYY-MM-DDTHH:mm:ss.sssZ. It is also like a new Date() constructor in default.

Syntax:

Date.toISOString()

Example:

const currentDate = new Date();
console.log(currentDate);

// Use the toISOString() method
console.log(currentDate.toISOString());

Output:

2022-10-07T12:03:52.852Z
2022-10-07T12:03:52.852Z

Use toUTCString()

The toUTCString() method will help you to return a string representing Date in UTC time zone format. UTC here stands for Universal Coordinated Time.

Example:

const currentDate = new Date();
console.log(currentDate);

// Use the toUTCString() method
console.log(currentDate.toUTCString());

Output:

2022-10-07T12:13:52.134Z
Fri, 07 Oct 2022 12:13:52 GMT

Summary

I have shown you how to get the current date and time in Typescript in this tutorial. You can use Date() constructor to get the current time. I have also shown you how to initial Date and format Date. Read this article carefully!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *