JavaScript Program to Convert Date to String
Last Updated :
20 Sep, 2023
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
- Using Date.toString() method
- Using String Constructor
- Using Date.toDateString()
- Using Date.toLocaleString() method
- Using Date.toGMTString() 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.
Javascript
let d = new Date()
let dateString = d.toString();
console.log(dateString)
|
Output
Wed Sep 20 2023 08:42:52 GMT+0000 (Coordinated Universal Time)
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.
Javascript
let d = new Date()
let dateString = String(d);
console.log(dateString)
|
Output
Wed Sep 20 2023 08:42:52 GMT+0000 (Coordinated Universal Time)
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.
Javascript
let d = new Date()
let dateString = d.toDateString();
console.log(dateString)
|
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.
Javascript
let d = new Date()
let dateString = d.toLocaleString(undefined,
{ timeZone: 'Asia/Kolkata' });
console.log(dateString)
|
Output
9/20/2023, 2:12:52 PM
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.
Javascript
let d = new Date()
let dateString = d.toGMTString();
console.log(dateString)
|
Output
Wed, 20 Sep 2023 08:42:52 GMT
Share your thoughts in the comments
Please Login to comment...