Home > DeveloperSection > Articles > Entering current date in existing html control by using java script

Entering current date in existing html control by using java script


JavaScript JavaScript 
Ratings:
0 Comment(s)
 4139  View(s)
Rate this:

Entering current date in existing html control by using java script

In this demonstration I will tell you that how to display date with different format and enter it in an existing html control like <p> (paragraph).

Write down following code snippet in notepad and save it with .html extension.

Code:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title></title>

    <script type="text/javascript">

        function displayDate() {

            var now = new Date();

            var message = "For MM/DD/YYYY format <br />";

            var mdyFormat = now.getMonth() + "/" + now.getDate() + "/" + now.getYear();

            message = message + mdyFormat + "<br />";

            message = message + "For DD/MM/YYYY format <br />";

            mdyFormat = now.getDate() + "/" + now.getMonth() + "/" + now.getYear() + "<br />";

            message = message + mdyFormat + "For YYYY/MM/DD format <br />";

            mdyFormat = now.getYear() + "/" + now.getMonth() + "/" + now.getDate();

            message = message + mdyFormat;

            document.getElementById('datedemo').innerHTML = message;

           

        }

    </script>

</head>

<body>

    <h1>Date Demonstration.</h1>

    <p id="datedemo">

       

    </p><br />

    <input type="button" value="Display Date" onclick="displayDate()" />

</body>

</html>

Output of the following code snippet is as follows

Current date in existing html control using java script


Don't want to miss updates? Please click the below button!

Follow MindStick