JavaScript Program to Convert Date to String
In this article, we will see how to convert a JavaScript Date to a string using JavaScript. We have a few methods to convert JavaScript Date to string which are described below.
Methods to Convert Date to String in JavaScript
Table of Content
Method 1: Using the Date.toString() method
JavaScript date.toString() method is used to convert the given Date object’s contents into a string.
Example: In this example, we will convert the date object to a string using the Date.toString() method.
// New date using date Constructor
let d = new Date()
// Convert to string
let dateString = d.toString();
// Print output
console.log(dateString)
Output
Wed Sep 20 2023 08:42:52 GMT+0000 (Coordinated Universal Time)
Method 2:Using String Constructor
The String constructor property in JavaScript is used to return the string constructor function for the object.
Example: In this example, we will convert the date object to a string using the String constructor.
// New date using date Constructor
let d = new Date()
// Convert to string
let dateString = String(d);
// Print output
console.log(dateString)
Output
Wed Sep 20 2023 08:42:52 GMT+0000 (Coordinated Universal Time)
Method 3: Using the Date.toDateString() method
The date.toDateString() method converts the given date object’s contents of the date portion into a string.
Example: In this example, we will convert the date portion of the object to a date string using the Date.toDateString() method.
// New date using date Constructor
let d = new Date()
// Convert to string
let dateString = d.toDateString();
// Print output
console.log(dateString)
Output
Wed Sep 20 2023
Method 4: Using Date.toLocaleString() method
The date.toLocaleString() method is used to convert a date and time to a string using the locale settings. The default settings for GMT.
Example: In this example, we will convert the date object to a string of locale format using the Date.toLocaleString() method.
// New date using date Constructor
let d = new Date()
// Convert to string
let dateString = d.toLocaleString(undefined,
{ timeZone: 'Asia/Kolkata' });
// Print output
console.log(dateString)
Output
9/20/2023, 2:12:52 PM
Method 5: Using Date.toGMTString() method
The date.toGMTString() method is used to convert the given date object’s contents into a string according to the Greenwich Mean Time GMT.
Example: In this example, we will convert the date object to a string using the Date.toGMTString() method.
// New date using date Constructor
let d = new Date()
// Convert to string
let dateString = d.toGMTString();
// Print output
console.log(dateString)
Output
Wed, 20 Sep 2023 08:42:52 GMT
Method 6: Using Date.toISOString() method
The toISOString() method in JavaScript converts a Date object to a string in ISO 8601 format (YYYY-MM-DDTHH:mm:ss.sssZ). This format is widely accepted and used for representing dates and times.
Example: We will convert a Date object to a string using the toISOString() method.
// New date using date Constructor
let d = new Date();
// Convert to string
let dateString = d.toISOString();
// Print output
console.log(dateString);
Output
2024-05-23T12:37:36.005Z
Method 7: Using Date.toUTCString() Method
The Date.toUTCString() method converts a Date object to a string, using the UTC time zone. This method returns a string that represents the date and time in the UTC time zone, formatted according to the Internet Greenwich Mean Time (GMT) convention.
Example: This example demonstrates how to convert a Date object to a string using the Date.toUTCString() method.
// Create a new Date object
let currentDate = new Date();
// Convert the Date object to a string using toUTCString() method
let utcString = currentDate.toUTCString();
// Output the result
console.log(utcString); // Output: Wed, 10 Jul 2024 14:48:34 GMT
Output
Wed, 10 Jul 2024 10:04:26 GMT
Method 8: Using Date.toJSON() Method
The Date.toJSON() method converts a Date object to a string formatted according to the ISO 8601 standard, similar to Date.toISOString(). This method is useful when you need to serialize a Date object to JSON.
Example: In this example, we will convert a Date object to a string using the Date.toJSON() method.
let currentDate = new Date();
let jsonString = currentDate.toJSON();
console.log(jsonString);
Output
2024-07-16T12:43:41.265Z