How to Convert String to Date in TypeScript ? Last Updated : 22 Aug, 2024 Comments Improve Suggest changes Like Article Like Report 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 ContentUsing new Date()Using Date.parse() Using 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 objectUsing 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.000ZUsing 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 Comment More infoAdvertise with us Next Article How to Convert String to Date in TypeScript ? G gauravggeeksforgeeks Follow Improve Article Tags : JavaScript Web Technologies TypeScript Similar Reads How to Compare Two Date Strings in TypeScript ? In TypeScript, we can compare two date strings by converting them to comparable formats using Date objects or by parsing them into the timestamps. We will discuss different approaches with the practical implementation of each one of them. Table of ContentUsing Date ObjectsUsing Date.parseUsing Intl. 4 min read How to Convert String to JSON in TypeScript ? Converting a string to JSON is essential for working with data received from APIs, storing complex data structures, and serializing objects for transmission. Below are the approaches to converting string to JSON in TypeScript:Table of ContentConvert String to JSON Using JSON.parse()Convert String to 5 min read How to Convert String to Number in TypeScript? In TypeScript, converting a string to a number is a common operation that can be accomplished using several different methods. Each method offers unique advantages and can be chosen based on the specific requirements of your application. Below are the approaches to convert string to number in TypeSc 4 min read How to Convert Typescript Dictionary to String ? In TypeScript, dictionaries are often represented as the objects where keys are associated with the specific values. Converting a TypeScript dictionary to a string is an important task when doing API calls where we cast the JSON response from the API to a string. Below are the ways to convert a Type 4 min read How to Convert String to Date Time in Scala? In this article, we will learn how to convert String to DateTime in Scala. Converting String to DateTime consists of parsing a textual representation of date and time into a structured DateTime object for further manipulation and processing in Scala programs. Table of Content Using SimpleDateFormatU 4 min read How to convert a JavaScript date to UTC? Javascript date can be converted to UTC by using functions present in the Javascript Date object. The toUTCString() method is used to convert a Date object into a string, according to universal time. The toGMTString() returns a string that represents the Date based on the GMT (UT) time zone. By defa 1 min read How to Format Date in TypeScript ? Formatting dates is important especially when displaying them to the users or working with date-related data. TypeScript provides various ways to achieve this. Below are the methods to format the date data type in TypeScript:Table of ContentUsing toLocaleString() methodUsing toLocaleDateString() met 3 min read How to express a Date Type in TypeScript ? In TypeScript, the Date object is used to handle dates and times. To express a Date type, declare a variable with the type annotation Date and assign it a date value. This allows for precise date manipulation and formatting in your TypeScript code.Ways to Express Date Type in TypeScriptTable of Cont 3 min read How to Calculate the Time Between 2 Dates in TypeScript ? In this article, we will calculate the time between two different dates using TypeScript. We will use the getTime() method to get the current time of the specified date. Syntax:const variable_name = new Date();const time = variable_name.getTime();Note: The getTime() method will return the time in th 2 min read Convert string into date using JavaScript In this article, we will convert a string into a date using JavaScript. A string must follow the pattern so that it can be converted into a date. A string can be converted into a date in JavaScript through the following ways: Table of Content Using JavaScript Date() constructorUsing JavaScript toDat 3 min read Like