Click to See Complete Forum and Search --> : Date & Time in Javascript


roman1980
December 12th, 2003, 03:22 PM
html
<head>
<body>

<script language="Javascript" type="text/javascript">
<!-- Hide script from old browsers

var myDate = new Date();
var myDay = myDate.getDay();
var myMonth = myDate.getMonth();
var myYear = myDate.getYear();
var myMinutes = myDate.getMinutes();
var myHours = myDate.getHours();
var mySeconds = myDate.getSeconds();

document.write("Time: " + myHours + ":" + myMinutes + ":" + mySeconds);

document.write("Date: " + myDay + "/" + myMonth + "/" myYear);
</script>
</body>
</head>
<html>


I want to display the time and date on a website. I have used the above code but nothing displays. Have I declared correctly as I am a beginner to XHTML and Javascript.

khp
December 12th, 2003, 04:05 PM
There is a very small bug in your code, you are missing a + operator, between "/" and myYear, in the last document.write line.

To find errors like this, I suggest using the javascript console tool, in mozilla/netscape7.

roman1980
December 12th, 2003, 06:13 PM
The output now is:

Time: 23:12:37Date: 5/11/2003

The time is fine but the date is not correct, any ideas?

khp
December 13th, 2003, 03:10 AM
The value returned by getDay is an integer corresponding to the day of the week: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.

The value returned by getMonth is an integer between 0 and 11. 0 corresponds to January, 1 to February, and so on.

You can see the full specifications for the Date Object here http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/date.html

roman1980
December 13th, 2003, 04:26 PM
Originally posted by khp
The value returned by getDay is an integer corresponding to the day of the week: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.

The value returned by getMonth is an integer between 0 and 11. 0 corresponds to January, 1 to February, and so on.

You can see the full specifications for the Date Object here http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/date.html

So do I need getDate instead of getDay so it will produce:

Time: 21:26:14Date: 13/11/2003

but why is 11, should it be 12 for the month?

khp
December 13th, 2003, 06:51 PM
Originally posted by khp
The value returned by getMonth is an integer between 0 and 11. 0 corresponds to January, 1 to February, and so on.

roman1980
December 14th, 2003, 06:54 AM
Originally posted by khp


How do I make it 1 to 12?

khp
December 14th, 2003, 08:15 AM
myDate.getMonth() +1