0

im new to java script. I am trying to make a time/date function update automatically. when i view the page i get the right time, but i have to refresh the browser in order to update it. How can i do it to automatically update itself.

i want the clients time not the servers time.

<script language="JavaScript" type="text/javascript">
var d=new Date();
var monthname=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var hours = d.getHours();
var minutes = d.getMinutes();
var sec = d.getSeconds();

var suffix = "AM"
if (hours >= 12)
	{
		suffix = "PM"
		hours = hours - 12;
	}
if (minutes < 10)
	minutes = "0" = minutes
	
//Ensure correct for language. English is "January 1, 2004"
var TODAY = monthname[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear() + " " + hours + ":" + minutes + ":" + sec + suffix

</script>
</head>
<body>
        <script language="JavaScript" type="text/javascript">
        document.write(TODAY);	</script>
</body>
</html>
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Graphix
0

You can use a interval to update the time every second. Also, every codeline needs to be closed with a ;

<html>
<head>
<script language="JavaScript" type="text/javascript">
function showDate() {
var date = getDate();
document.getElementById("time_date").innerHTML = date;
}

function getDate() {
var d=new Date();
var monthname=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var hours = d.getHours();
var minutes = d.getMinutes();
var sec = d.getSeconds();

var suffix = "AM";
if (hours >= 12)
	{
		suffix = "PM";
		hours = hours - 12;
	}
if (minutes < 10) {
	minutes = "0" + minutes;
}
	
//Ensure correct for language. English is "January 1, 2004"
var TODAY = monthname[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear() + " " + hours + ":" + minutes + ":" + sec + suffix;
return TODAY;
}
window.onload = setInterval("showDate()",1000);
</script>
<title>My Page</title>
</head>
<body>
<div id="time_date"></div>
</body>
</html>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.