How to Convert a Date Object to a String in JavaScript?

To convert a date object to a string, call the toString() method on the date object which you want to convert into a date string. The toString() method when called on a date object, returns a new string representing the date-time in a human-readable format.

The returned date string consists of the day of the week, month, day, year, time, and the timezone offset.

// Get current date object
const date = new Date();

// Convert date object to date string
const dateString = date.toString();

console.log(dateString);
// Output: 👉 "Tue Mar 14 2023 20:40:19 GMT-0700 (Pacific Daylight Time)"

console.log(typeof dateString);
// Output: 👉 "string"

In this example, we first get the current date-time object by calling the date constructor new Date().

The date constructor new Date() returns the current date-time as a date object.

To convert the date object returned from the date constructor, we called the toString() method which converts the given date object into its equivalent date string.

Note that the toString() method works on all date objects, be it the current date-time object or any specific date object.

For getting a specific date object, you can pass the year, month index, and day to the date constructor and then call the toString() method to get the string version of the date object.

For example:

// Get specific date object
const date = new Date(2022, 1, 14);

// Convert date object to date string
const dateString = date.toString();

console.log(dateString);
// Output: 👉 "Mon Feb 14 2022 00:00:00 GMT-0800 (Pacific Standard Time)"

console.log(typeof dateString);
// Output: 👉 "string"

Please note that the month indexes are 0-based. Jan – 0, Feb – 1, and so on.


2. Use the toISOString() Method to Convert the Date Object to a String

If you want to convert the date object to an ISO string, you can call the toISOString() method on the given date object.

The toISOString() method returns a string representing the date and time in the ISO format.

For example:

// Get current date-time object
const date = new Date();

// Convert date object to ISO string
const dateString = date.toISOString();

console.log(dateString);
// Output: 👉 "2023-03-15T04:19:43.575Z"

console.log(typeof dateString);
// Output: 👉 "string"

3. Use the toLocaleString() Method to Convert the Date Object to a String

If you want to convert the date object to a locale date string i.e. date and time in a format that is appropriate for the user’s locale, including the language, date, and time formatting, then you can call the toLocaleString() method instead of traditional toString() method.

For example:

// Get current date-time object
const date = new Date();

// Convert date object to Locale date string
const dateString = date.toLocaleString();

console.log(dateString);
// Output: 👉 "3/14/2023, 9:32:50 PM"

console.log(typeof dateString);
// Output: 👉 "string"

You can pass an optional locale parameter to the toLocaleString() method to format the date string according to a specific locale.

For example:

// Get current date-time object
const date = new Date();

// US English format
const dateString1 = date.toLocaleString('en-Us');

console.log(dateString1);
// Output: 👉 "3/14/2023, 9:42:41 PM"

// British English format
const dateString2 = date.toLocaleString('en-GB');

console.log(dateString2);
// Output: 👉 "14/03/2023, 21:42:41"

Conclusion

In this article, we learned different ways of converting a date object to a string in JavaScript.

JavaScript has a bunch of different built-in methods that you can call on any date object to convert it into a string.

Each method returns a date string in a different date format. For example, if you want the date string in ISO format, you can call the toISOString() method.

If you need even more control, for example, you want the date string according to the location of the user, you use the toLocaleString() method.

That’s all for the day. Happy Coding.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.