toDateString() : Date object to string

To convert date object to a string we have to use toDateString function. The output we will get is a string. Here is the syntax
Date.toDateString
Here is an example.
<script type="text/javascript"> 
var dt= new Date();
document.write(dt + "<br><br>");
document.write(dt.toDateString());
</script>
The output of the above code is here.

Note that before applying any string functions to date object we must convert it to string. The line bellow will generate error message
document.write(dt.substr(5)); // error 
dt in above code is a date object so we can't use like this

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 object to local date 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