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.
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.
The constructor is not taking any parameters. It will create one Date object with current date and time.
We can pass the number of milliseconds since January 1, 1970, 00:00:00 UTC.
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.
If we pass a date object, it creates a copy of that date object with equivalent data and returns that object.
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.
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:
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
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:
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.
- Typescript any explanation with example
- How to create a class in typescript with example
- How to convert any to string in typescript
- TypeScript string search method
- How to create an array of objects in TypeScript
- TypeScript program to add two numbers
- 4 ways in TypeScript to reverse a string
- How to compile and run a TypeScript program