Horje
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.

Using new Date()

In this approach, we are using the new Date() constructor in TypeScript to convert a string representation into a Date object. The output Date object represents the date and time parsed from the input string, and it is then printed to the console.

Syntax:

let currentDate: Date = new Date();

Example: The below example uses new Date() to convert string to date in TypeScript.

JavaScript
let dStr: string = "2024-02-27";
let res: Date = new Date(dStr);
console.log(res, typeof res);

Output:

2024-02-27T00:00:00.000Z object

Using Date.parse()

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.

JavaScript
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

Using Date.UTC()

In this approach, we are using Date.UTC() to create a UTC timestamp based on the individual components parsed from the input string. The temp array holds the parsed year, month, and day, and a new Date object (res) is then constructed using Date.UTC(). The output Date object displayes the parsed date in Coordinated Universal Time (UTC).

Syntax:

let timestamp: number = 
Date.UTC(year, month[, day[, hour[, minute[, second[, millisecond]]]]]);

Example: The below example uses Date.UTC() to convert string to date in TypeScript.

JavaScript
let dStr: string = "2024-02-27";
let temp: number[] = 
    dStr.split('-').map(Number);
let res: Date = 
    new Date(Date.UTC(temp[0], 
    temp[1] - 1, temp[2]));
console.log(res);

Output:

2024-02-27T00:00:00.000Z

Approach 4: Using the moment.js library

The moment.js library provides powerful utilities for parsing, validating, manipulating, and formatting dates and times in JavaScript. You can use moment.js to easily convert a string representation of a date to a Date object in TypeScript.

Example: In the following example, we’ll use moment.js to convert a string to a Date object in TypeScript.

JavaScript
import moment from 'moment';

let dateString: string = "2024-02-27";
let dateObj: Date = moment(dateString).toDate();
console.log(dateObj);

Output

2024-02-27T00:00:00.000Z

Approach 5: Using Date-fns Library

The Date-fns library is a modern JavaScript date utility library that provides comprehensive date and time manipulation capabilities. It offers a clean and simple API, making it an excellent choice for working with dates in TypeScript.

Example: The following example demonstrates how to use the Date-fns library to convert a string to a Date object in TypeScript.

JavaScript
import { parseISO } from 'date-fns';
let dateString: string = "2024-02-27T00:00:00.000Z";
let parsedDate: Date = parseISO(dateString);
console.log(parsedDate);
console.log(typeof parsedDate);

Output:

2024-02-27T00:00:00.000Z
object



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
JavaScript Program to Copy all Nodes of a Linked List JavaScript Program to Copy all Nodes of a Linked List
How to Format the Labels of the Y Axis in a Chart ? How to Format the Labels of the Y Axis in a Chart ?
JavaScript Program to Find N Largest Elements from a Linked List JavaScript Program to Find N Largest Elements from a Linked List
How to Create Interface with Self-Reference Property in TypeScript ? How to Create Interface with Self-Reference Property in TypeScript ?
How to Create an Interface with Generic Type? How to Create an Interface with Generic Type?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11