How to Convert String to Date in TypeScript?

As a TypeScript developer, I frequently receive requirements for converting a string to a date. There are various methods for doing so. In this tutorial, I will explain how to convert a string to a date in TypeScript using different approaches with examples.

Converting strings to dates allows you to manipulate and display dates more effectively. For instance, you might have a date string from a user input or an API response that needs to be converted into a Date object for further processing.

Basic Date Conversion in TypeScript

TypeScript, being a superset of JavaScript, inherits JavaScript’s robust date handling capabilities. The simplest way to convert a string to a date is by using the Date constructor.

Example 1: Basic Conversion

let dateString: string = "2025-11-14T10:20:30Z";
let dateObject: Date = new Date(dateString);
console.log(dateObject); // Outputs: Thu Nov 14 2025 10:20:30 GMT+0000 (Coordinated Universal Time)

In this example, the string dateString is in ISO 8601 format, which is a standard format recognized by the Date constructor. This method works well for most standard date formats.

Here is the exact output in the screenshot below:

Convert String to Date in TypeScript

Check out Check if a String Contains a Substring in TypeScript

Handle Different Date Formats in TypeScript

Sometimes, date strings might not be in a standard format. In such cases, you need to parse the string manually or use libraries like Moment.js or date-fns.

Example 2: Custom Date Format

Consider a scenario where you receive a date string in the format “MM/DD/YYYY HH:mm:ss”, which is common in the USA.

let dateString: string = "11/14/2025 10:20:30";
let [datePart, timePart] = dateString.split(" ");
let [month, day, year] = datePart.split("/");
let [hours, minutes, seconds] = timePart.split(":");

let dateObject: Date = new Date(+year, +month - 1, +day, +hours, +minutes, +seconds);
console.log(dateObject); // Outputs: Thu Nov 14 2025 10:20:30 GMT-0500 (Eastern Standard Time)

In this example, we manually parse the date string and create a Date object using the parsed values.

Using Libraries for Date Parsing

While manual parsing works, it can be error-prone and tedious. Libraries like Moment.js and date-fns simplify date parsing and formatting. Here is how you can use Moment.js to parse a date in TypeScript.

Example 3: Using Moment.js

Moment.js is a popular library for handling dates in JavaScript and TypeScript. Here’s how you can use it to parse a date string.

import * as moment from 'moment';

let dateString: string = "11/14/2025 10:20:30";
let dateObject: Date = moment(dateString, "MM/DD/YYYY HH:mm:ss").toDate();
console.log(dateObject); // Outputs: Thu Nov 14 2025 10:20:30 GMT-0500 (Eastern Standard Time)

In this example, we use Moment.js to parse the date string according to the specified format and convert it to a Date object.

Example 4: Using date-fns

date-fns is another powerful library for date manipulation. It is lightweight and modular, making it a great choice for modern applications.

import { parse } from 'date-fns';

let dateString: string = "11/14/2025 10:20:30";
let dateObject: Date = parse(dateString, "MM/dd/yyyy HH:mm:ss", new Date());
console.log(dateObject); // Outputs: Thu Nov 14 2025 10:20:30 GMT-0500 (Eastern Standard Time)

In this example, we use the parse function from date-fns to convert the date string to a Date object.

Check out Convert a String to Boolean in TypeScript

Handle Time Zones While Parsing Date in TypeScript

When dealing with dates, it’s essential to consider time zones, especially if your application serves users across different regions.

Example 5: Time Zone Handling

Let’s say you receive a date string in a specific time zone and need to convert it to a Date object in UTC.

Then you can use the below TypeScript code.

let dateString: string = "11/14/2025 10:20:30 GMT-0500";
let dateObject: Date = new Date(dateString);
console.log(dateObject.toISOString()); // Outputs: 2025-11-14T15:20:30.000Z

In this example, the date string includes the time zone offset, and the Date constructor correctly converts it to UTC.

Read Convert String to Number in TypeScript

Common Issues and Best Practices

Now, let me explain some common issues you might face while converting string to date in TypeScript.

Issue 1: Invalid Date Formats

Not all date strings are valid. Always validate the input before attempting to parse it.

let dateString: string = "invalid date";
let dateObject: Date = new Date(dateString);

if (isNaN(dateObject.getTime())) {
  console.error("Invalid date format");
} else {
  console.log(dateObject);
}

Issue 2: Browser Differences

Different browsers may interpret date strings differently. Stick to standard formats or use libraries for consistency.

Best Practice: Use Libraries

For complex date manipulations and parsing, always prefer libraries like Moment.js or date-fns. They handle edge cases and provide a consistent API.

Conclusion

In this tutorial, I have explained how to convert strings to dates in TypeScript using various methods and libraries. You can use the built-in Date constructor, or also you can use powerful libraries like Moment.js and date-fns, etc. Following the examples and best practices I have explained, you can effectively handle various date formats and time zones in TypeScript. Do let me know if you have any questions in the comment below.

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.