?
Solved

Javascript Clock **URGENT EASY 500 POINTS**

Posted on 2006-04-04
9
Medium Priority
?
214 Views
Last Modified: 2007-12-19
I need a javascript clock. I want it to display the time like this:

5:35pm April 4, 2006

but instead of getting the time and date from the user's computer I want to have it start at a date and time that I specify. Also I want it to count so on the page it is live and it isn't in a text box it is just plain text.

Max
0
Comment
Question by:TheMaximumWeasel
  • 4
  • 4
9 Comments
 
LVL 9

Accepted Solution

by:
Lakio earned 2000 total points
ID: 16377577
<html>
<head>
<title>Lakio's Timemover</title>
<script>
var when = new Date('5:35pm April 4, 2006'); // just insert the date and time here

function clock(){
var mo=['January','February','March','April','May','June','July','August','September','October','November','December'];
var ap=when.getHours()?'pm ':'am ';
var ho=when.getHours(); ho=ho>12?ho-12:ho;

document.getElementById('clock').innerHTML=ho+when.toUTCString().match(/:\d\d/)+ap+mo[when.getMonth()]+' '+when.getDate()+', '+when.getUTCFullYear();

when=new Date(Date.parse(when)+60000);
}
</script>
</head>
<body onload="clock();self.setInterval('clock()', 60000);">
<div id="clock"></div>
</body>
</html>
0
 
LVL 14

Expert Comment

by:sam2912
ID: 16423564
Lakio's answer is OK, however I suggest that the time be updated every second instead:

<body onload="clock();self.setInterval('clock()', 1000);">
0
 
LVL 9

Expert Comment

by:Lakio
ID: 16432077
btw: its set for every min
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:sam2912
ID: 16432626
I know, but you want it to be accurate to the second...
0
 
LVL 9

Expert Comment

by:Lakio
ID: 16432720
if we are displaying min not sec we dont need that,

new Date('5:35pm April 4, 2006')
is the same as new Date(2006, 3, 4, 17, 35, 0, 0) (yr_num, mo_num, day_num , hr_num, min_num, sec_num, ms_num)
so sec are set a 0 ok!?

and sins the next min is after 60sec its no need to run the script 60times for nothing,
thats just becuse we dont show sec but if we did then we would run it every sec.

In short: It is accurate to the second
0
 
LVL 14

Expert Comment

by:sam2912
ID: 16433347
But the first time the script is run, the update will be between 60 secs to 119 seconds.
0
 
LVL 9

Expert Comment

by:Lakio
ID: 16437990
"But the first time the script is run, the update will be between 60 secs to 119 seconds."
I dont understand your point but here is a script that is very very accurate.

and still just runs every min

On page load the time is set to '5:35pm April 4, 2000' and it does not matter how often the script is run or if the cpu is under alot of load. the time is connected the the cpu clock so its alwas "running".


<html>
<head>
<title>Lakio's Timemover v2.0</title>
<script>
diff = new Date()-new Date('5:35pm April 4, 2000'); // just insert the date and time here
function clock(){var when=new Date(new Date()-diff);
var mo=['January','February','March','April','May','June','July','August','September','October','November','December'];
var ap=when.getHours()?'pm ':'am ';
var ho=when.getHours(); ho=ho>12?ho-12:ho;

document.getElementById('clock').innerHTML=ho+when.toUTCString().match(/:\d\d/)+ap+mo[when.getMonth()]+' '+when.getDate()+', '+when.getUTCFullYear();
}
</script>
</head>
<body onload="clock();self.setInterval('clock()', 60000);">
<div id="clock"></div>
</body>
</html>
0
 
LVL 14

Expert Comment

by:sam2912
ID: 16442846
ok, won't argue... cos it still generally works. It was just a suggestion in the first place.
0
 
LVL 4

Author Comment

by:TheMaximumWeasel
ID: 16465523
I already found one online and edited it for my  site so I will split all of the points to the people who provided an acceptable answer
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question