How to use Date.parse() In Typescript
In this approach, we are using Date.parse() to convert the string representation of a date into the corresponding timestamp. Then, a new Date object is created using the got timestamp, representing the parsed date and time, and it is printed to the console.
Syntax:
let timestamp: number =
Date.parse(dateString);
Example: The below example uses Date.parse() to convert string to date in TypeScript.
let dStr: string = "February 27, 2024 ";
let time: number = Date.parse(dStr);
let res: Date = new Date(time);
console.log(res);
Output:
2024-02-26T18:30:00.000Z
How to Convert String to Date in TypeScript ?
In TypeScript, conversion from string to date can be done using the Date object and its method.
We can use various inbuilt methods of Date object like new Date() constructor, Date.parse(), and Date.UTC.
Table of Content
- Using new Date()
- Using Date.parse()
- Using Date.UTC()
- Using the moment.js library