Results 1 to 2 of 2

Thread: Time and Date script

  1. #1
    Join Date
    Jun 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Time and Date script

    Time and Date JavaScript

    This is my first Javascript
    A simple time and date Javascript!

    <html>
    <head>
    <script type="text/javascript">
    function displayDate()
    {
    document.getElementById("demo").innerHTML=Date();
    }
    </script>
    </head>
    <body>

    <h1>Time and Date</h1>
    <button type="button" onclick="displayDate()">Display Date</button>

    </body>
    </html>

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Nice for a first time. Are you posting to show us or because it doesn't work?

    To make it work the Date() function has to return something. Give this a try:
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<script type="text/javascript">
    	function returnDate(){
    		var now = new Date(); // this returns the amount of milliseconds since 1/1/1970, which is how we'll get the current date
    		var month = now.getMonth()+1; // just to get the month, all we do is use the date object we've initialized, and call the getMonth function. But, because the months are indexed to 0 (meaning months are numbered 0-11), we have to add 1
    		var day = now.getDate(); // get the day
    		var year = now.getYear(); // get the year
    		
    		return month + "/" + day + "/" + year;
    		
    	}
    	function displayDate() {
    		document.getElementById("demo").innerHTML = returnDate();
    	}
    	</script>
    </head>
    <body>
    	<h1>Time and Date</h1>
    	<time id="demo">mm/dd/yyyy</time><input type="button" onclick="displayDate()" value="Display date">
    </body>
    </html>
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •