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>

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>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.