toString() :Converting Date object to String

Before applying any string functions to the date object we must convert it to string by using toString function.
Date.toString();
Sample code
<script type="text/javascript"> 
var dt= new Date();
document.write(dt + "<br><br>");
document.write(dt.toString());
</script>
The output is here

After conversion to string we can apply any string function like this .
document.write(dt.toString().substr(5));

Difference between toDateString() and toString()

toDateString(): Returns only the date part of a Date object in a human-readable format (e.g., "Mon Sep 06 2021"). It excludes the time and timezone information.

toString(): Returns a full string representation of the Date object, including the day, date, time, and timezone (e.g., "Mon Sep 06 2021 14:20:30 GMT+0000 (UTC)").
let date = new Date();
console.log(date.toDateString()); // "Mon Sep 06 2021"
console.log(date.toString());     // "Mon Sep 06 2021 14:20:30 GMT+0000 (UTC)"

Change the date to local string
Date Reference
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer