PDA

View Full Version : Time and Date script



Bucketbee
06-07-2011, 03:59 PM
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>

Nile
06-08-2011, 12:39 AM
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:



<!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>