Languages
[Edit]
EN

TypeScript - create date object

0 points
Created by:
Dollie-Rutledge
806

In this article, we would like to show you how to create date object in TypeScript.

Quick solution:

 

1. Current date

Edit

In this example, we use Date() constructor without any arguments to create a Date object initialized with the current datetime. Then we display the current datetime in ISO format.

Example output:

2. Date from date string

Edit

In this example, we use Date() constructor with one argument to create the date object from a date string in a proper format. Then we display the date in ISO format.

Output:

3. Date from Unix time

Edit

In this example, we use Date() constructor with one argument to create the date object from unix time. To do so, we need to convert timestamp to milliseconds by multiplying it by 1000. Then we display the date in ISO format.

Output:

4. Date from specified parameters

Edit

In this example, we use Date() constructor with many arguments to create the date object from specified data. Then we display the date in ISO format.

Syntax:

Practical example: 

Output:

Note:

The hour in ISO format may be different depending on the time zone you live in. My time is currently given in GMT+0200 (Central European Summer Time), that's why ISO format subtracts 2 hours (from 15 to 13).

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join