In JavaScript, every object has a toString() method to represent the information in a text or string format. Likewise, the date object is used with the toString() method when a user is required to present the date in a string format.
Modern browsers such as Opera, Firefox, Chrome, Firefox, etc. support the date.toString() method. In this post, we will give a concise overview of the toString() method and its usage.
How to Use the Date toString() Method in JavaScript?
The syntax of the date.toString() method is mentioned below:
Syntax
The Date toString() method will not take any input but returns the string value. Let’s experience how it works using a few examples.
Example 1: Get the Current Date/Time as a String Value
The current date and time can be obtained as a string value. A simple code that serves the purpose is provided below:
Code
<body>
<p> Example 1: Using the Date toString() method</p>
<p> Press button to display the date and time as a string.</p>
<button onclick="myFunction()">Press it</button>
<p id="test"></p>
<script>
function myFunction() {
var date = new Date();
var string = date.toString();
document.getElementById("test").innerHTML = string;
}
</script>
</body>
</html>
The description of the above code is provided here:
-
- A function named myFunction() is created that will be called on the onclick event of the button.
- In the myFunction(), an object of the Date() is created. The Date() method contains the current date/time.
- After that, the toString() method is applied on the object named date and the result is stored in a string variable.
- In the end, the string is assigned to the HTML element (whose id=test) for display in the browser.
Output
The outcome of the above code is presented here. Upon pressing the button, the current date, day, and time is displayed on the browser.
Example 2: To Convert the User Defined Date to String
Another example is given here by employing the toString() method. This method converts the information into the string format. In this way , the code is as follows.
<body>
<script>
// Example 2: Using the Date toString() method
var date=new Date(2021, 3, 23, 22, 21, 30);
document.writeln(date.toString());
</script>
</body>
</html>
In this example, numerical values are passed to the built-in Date() method. These values are saved in the date object. After that, the toString() method is utilized to transform the numerical information into the string format. In the end, using the document.writeln() method is utilized to display the string format in the browser.
Note: While using the date.toString() method, the 0 refers to the January and 11 represents the December month. In our case, we have used 3 (as the month value) which refers to the April month.
Output
In the output, the value presented in the date object is converted into string and is printed on the browser.
Conclusion
In JavaScript, the date.toString() method is utilized to display the date and time in a string format. The Date toString() method is applied on the date object. The date.toString() method does not accept any arguments and just converts the provided date/time to a string. This post briefly demonstrates the purpose and working of the date.toString() method in JavaScript. For better understandability, different examples are also provided.