SRL Archive - Web Development

need help with JS (Date/time).


Laimonas171

2010-11-11 14:01:04

Hello guys, I am totaly newbie on JavaScript, and I need to make quickly one timer… So I'm asking for help :)

I need timer witch one starts from 2010 October 14 17:00 (5PM) and goes from 0 never stops like chronometer. it should look sth like.

today should be sth like 0years 0month 29 days 4hours 0mins 34sec (and bold part should increase all the time each sec or better milisec )

anyone can help? :/

Camaro'

2010-11-11 23:23:48


<html>
<head>
<script type="text/javascript">
var c=0;
var t;
var timer_is_on=0;

function secondsToString(seconds)
{

var numdays = Math.floor(seconds / 86400);
var numhours = Math.floor((seconds % 86400) / 3600);
var numminutes = Math.floor(((seconds % 86400) % 3600) / 60);
var numseconds = ((seconds % 86400) % 3600) % 60;




if (numdays > 0){
return numdays + " days " + numhours + " hours " + numminutes + " minutes " + numseconds + " seconds";
}

if (numhours > 0) {
return numhours + " hours " + numminutes + " minutes " + numseconds + " seconds";
}

if (numminutes > 0) {
return numminutes + " minutes " + numseconds + " seconds";
}


return numseconds + " seconds"




}

function timedCount()
{

document.getElementById('change').innerHTML = secondsToString(c);
c=c+1;
t=setTimeout("timedCount()",1000);
}

function doTimer()
{
if (!timer_is_on)
{
timer_is_on=1;
c = 0;
timedCount();
}
}
</script>
</head>

<body>
<b id='change'>Click button to start</b>
<form>
<input type="button" value="Start count!" onClick="doTimer()">

</form>
<p>Click on the button above. The input field will count forever, starting at 0.</p>
</body>
</html>

Does not do months or years :(

Laimonas171

2010-11-12 07:54:16

[QUOTE=Camaro';749252][code]
<html>
<head>
<script type="text/javascript">
var c=0;
var t;
var timer_is_on=0;

function secondsToString(seconds)
{

var numdays = Math.floor(seconds / 86400);
var numhours = Math.floor((seconds % 86400) / 3600);
var numminutes = Math.floor(((seconds % 86400) % 3600) / 60);
var numseconds = ((seconds % 86400) % 3600) % 60;




if (numdays > 0){
return numdays + " days " + numhours + " hours " + numminutes + " minutes " + numseconds + " seconds";
}

if (numhours > 0) {
return numhours + " hours " + numminutes + " minutes " + numseconds + " seconds";
}

if (numminutes > 0) {
return numminutes + " minutes " + numseconds + " seconds";
}


return numseconds + " seconds"




}

function timedCount()
{

document.getElementById('change').innerHTML = secondsToString©;
c=c+1;
t=setTimeout("timedCount()",1000);
}

function doTimer()
{
if (!timer_is_on)
{
timer_is_on=1;
c = 0;
timedCount();
}
}
</script>
</head>

<body>
<b id='change'>Click button to start</b>
<form>
<input type="button" value="Start count!" onClick="doTimer()">

</form>
<p>Click on the button above. The input field will count forever, starting at 0.</p>
</body>
</html>
Does not do months or years :(

Ok thanks i will try to anilize code :)

Camaro'

2010-11-15 22:57:55

You could also use

http://www.w3schools.com/js/tryit.asp?filename=tryjs_text

and just replace whatever is in there with the code I posted to test. :)