Q_24505667: Showing today's date in javascript and onload

This example shows how to show today's date onload and after a click on a button (see below). Click refresh in your browser to refresh the display and start again.

This page will remain online for future reference. The original question can be viewed at Experts-Exchange, but requires (free) membership. More information about me, about how this site is maintained and about how to use these pages for yourself, check out the opening page.

Use the following code:

function returnDate(){
    var dDate = new Date();
    var dy = dDate.getDate();
    var mo = dDate.getMonth() + 1;
    var yr = dDate.getYear() + 1900;
    
    return mo + '/' + dy + '/' + yr;
}
function showDate(elemId){
    elem = document.getElementById(elemId);
    elem.innerHTML = returnDate();
}

<!-- use this hmtl code -->
<input type="button" onclick="showDate('myDate2')" value="show date" />
<span id="myDate2">
</span>
 
	

below this, in a little red square, you should see the date of today