1

I was asked to create a clock with timing and date event.... i want the function create a clock with timing and event and post the value together.I want the clock with timing and date event to be like this below.enter image description here

My requirement is to create a clock with timing and date event. The time must change continuously.The date must change as well. If I run the html website today at 23:59:59, then the date must change to tommorow. Don't need to change the timezone just add the label SGT..Post the clock with timing and date event next to the local time. Here is my code below....

<html>
<head>
<title>Clock with a timing event</title>
<script type="text/javascript">
function startTime() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById('txt').innerHTML =
    h + ":" + m + ":" + s;
    var t = setTimeout(startTime, 500);
}

function checkTime(i) {
    if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
    return i;
}
</head>
<body>
<table border="1">
<tr>
<td>
Local Time
</td>
<td>
values
<td>
</tr>
</body>
</html>
  • Well you never call `startTime`... so it isn't going to do anything till you do – Patrick Evans Sep 08 '17 at 03:55
  • @PatrickEvans, I know that I did not call startTime. But I want to know on how to create Date event.. –  Sep 08 '17 at 03:58
  • There is no such thing as a Date event, unless you mean creating a custom event yourself. But there is no such thing as like `ondate` – Patrick Evans Sep 08 '17 at 03:59
  • @PatrickEvans, you mean i cannot create a clock with the date???? For example, the time now is 23:59:59, after 1 seconds the time will be 00:00:00 and the date will change as well –  Sep 08 '17 at 04:01
  • Your startTime function does that, new Date() always gets the current time – Patrick Evans Sep 08 '17 at 04:01
  • if i want the date to change according to the time, then how –  Sep 08 '17 at 04:03
  • You are already doing it.... by having a setTimeout call startTime its going to update the element every 0.5 seconds.... – Patrick Evans Sep 08 '17 at 04:04
  • so that is how is it... I see................Is it best to setTimeout every 5 seconds???? Should I set to 1 seconds –  Sep 08 '17 at 04:05
  • not 5 seconds, 0.5 seconds as in 500 milliseconds. – Patrick Evans Sep 08 '17 at 04:08
  • oops my bad............ thank a lot @PatrickEvans I am too blur –  Sep 08 '17 at 04:11
  • You might want to read [*Where can I find documentation on formatting a date in JavaScript?*](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – RobG Sep 08 '17 at 05:04
  • @PatrickEvans, if i want to input the milliseconds (for example "12:07:21:123"), then how much milliseconds should i implement for the timeout –  Sep 08 '17 at 08:02

0 Answers0