JavaScript Date.toString() Method

MD Niaz Rahman Khan Jan 30, 2023
  1. Syntax of JavaScript date.toString() Method
  2. Example Codes: Use the date.toString() Method
  3. Example Codes: Use the date.toString() Method Automatically
JavaScript Date.toString() Method

In JavaScript, the Date object is used to represent the Date and Time. We can declare this object with the new keyword.

The toString() method comes internally in JavaScript and represents an object in the string. The date.toString() method converts the Date object to date and time and represents it as a string.

Syntax of JavaScript date.toString() Method

date.toString()

Parameters

The date.toString() method doesn’t accept any parameter.

Return

A date and time string value are returned from the Date object.

Example Codes: Use the date.toString() Method

const date = new Date()
let dateString = date.toString()

console.log(dateString)

Output:

Thu Sep 8 2022 12:20:33 GMT+0600 (Bangladesh Standard Time)

The Date object has been declared in the date variable. We used the toString() method to represent date and time as a string.

This method returned the date and time as a string in the local time zone. To store the result, we declared a variable and printed the variable to see the result in the output.

Example Codes: Use the date.toString() Method Automatically

const date = new Date()
let dateString = `Today's Date and Time is ${new Date()}`

console.log(dateString)

Output:

Today's Date and Time is Thu Sep 8 2022 12:27:33 GMT+0600 (Bangladesh Standard Time)

The toString() method is automatically called when we have called the Date object inside a string.

MD Niaz Rahman Khan avatar MD Niaz Rahman Khan avatar

Niaz is a professional full-stack developer as well as a thinker, problem-solver, and writer. He loves to share his experience with his writings.

LinkedIn

Related Article - JavaScript Date