Javascript Data Type How to - Display Local time and local date String








Question

We would like to know how to display Local time and local date String.

Answer


 <!--from   w w w. j a v a  2s . c  o m-->

<html>
<body>
    <p id="dateField">&nbsp;</p>
    <script type = "text/javascript">

    var myDate = new Date();
    var dateString = myDate.toLocaleDateString() + " " + myDate.toLocaleTimeString();
    var dateLoc = document.getElementById("dateField");
    dateLoc.innerHTML = "Now: " + dateString;
    </script>

</body>
</html>

The code above is rendered as follows: