articles

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

Anonymous User7807 13-Mar-2011

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

Entering current date in existing html control by using java script


Updated 04-Mar-2020
I am a content writter !

Leave Comment

Comments

Liked By