Click to See Complete Forum and Search --> : Clock Script


tims15
10-07-2005, 10:54 AM
Does anyone know of a script where is shows the time minus about 10 mins? I want it to update evey minute, but only on page loads.

Cheers

webstuff
10-07-2005, 11:00 AM
Calculate the time in 'ticks' then deduct 10mins worth of 'ticks' from the current time as 'ticks' then convert the 'ticks' into a time, the result should be a time which is 10 mins less than the current time.

konithomimo
10-07-2005, 11:14 AM
Use the script code below:


var realTime=new Date(); //actual time and date
var h=realTime.getHours();//current hour
var m=realTime.getMinutes();//current minutes
var s=realTime.getSeconds();//current seconds

realTime.setMinutes(realTime.getMinutes() - 10); //Make minutes display as actual minutes minus 10 minutes.

alert (realTime.toTimeString())


That will make the variables what you want. Then just do the display for it.

Charles
10-07-2005, 11:14 AM
<script type="text/javascript">
then = new Date()
then.setMinutes (then.getMinutes() - 10)
alert (then.toTimeString())
</script>

webstuff
10-07-2005, 11:23 AM
AND if the number of minutes is less than 10 past the hour, you will end up with negative minutes.

Thats why I suggested the TICKS approach, then that way you will NEVER end up with a negative time value.

konithomimo
10-07-2005, 11:58 AM
AND if the number of minutes is less than 10 past the hour, you will end up with negative minutes.

Thats why I suggested the TICKS approach, then that way you will NEVER end up with a negative time value.

Then just insert in an if statement saying that if the actual minutes is less than 10 add fifty to it and assign that value to be the minutes. Else just subtract 10.

Such as:

var realTime=new Date(); //actual time and date
var h=realTime.getHours();//current hour
var m=realTime.getMinutes();//current minutes
var s=realTime.getSeconds();//current seconds

if (m < 10)
{
realTime.setMinutes(realTime.getMinutes() + 50); //minutes - 10
}
else
{
realTime.setMinutes(realTime.getMinutes() - 10); //minutes - 10
}


Now you won't get negative numbers.

tims15
10-07-2005, 12:28 PM
Thanks for your replys. I can't seem to get it to display the time on a webpage- any ideas on what Im doing wrong?

webstuff
10-07-2005, 12:31 PM
so

var nowTicks = new Date().getTime()
var tenMinsTicks =10*60*1000;
var timeDiff = new Date(nowTicks - tenMinsTicks);
newDisplayTime=timeDiff.getHours() + ":" + timeDiff.getMinutes() + "." + timeDiff.getSeconds();
document.write(newDisplayTime);

is all a bit too much then? and no logic needed.

konithomimo
10-07-2005, 12:44 PM
I just showed you how to change the time, not how to display it. My code with the display is:


var realTime=new Date(); //actual time and date
var h=realTime.getHours();//current hour
var m=realTime.getMinutes();//current minutes
var s=realTime.getSeconds();//current seconds
var adjust= -10; //number of minutes to adjust backwards
//this is the only number you have to ever change

var underadjust=(60+adjust);
var overadjust = (m+adjust);


if (adjust < 0)
{
if (m < 10)
{
realTime.setMinutes(realTime.getMinutes() + underadjust); //minutes - 10
realTime.setHours(realTime.getHours() - 1);//have to fix hour also
}
else
{
realTime.setMinutes(realTime.getMinutes() + adjust); //minutes - 10
}
}

else
{
if (overadjust >60)
{
realTime.setMinutes(overadjust - 60);
realTime.setHours(realTime.getHours() + 1);
}

else
{
realTime.setMinutes(realTime.getMinutes() + adjust);
}
}

var CorrectedTime=realTime.getHours() + ":" + realTime.getMinutes() + ":" + realTime.getSeconds();
document.write(CorrectedTime);


I edited mine to work for adding time and subtracting it. I did it very sloppily and should clean it up,but I dont have the time.

webstuff
10-07-2005, 12:52 PM
you miss the point, the code I provide will work without error and provide exactly the desited result without any convoluted methods of having to test for and account for the time calculations, my code works best because it has been reduced to a single format that can easily be modified without any further calculation via the use of logic.

I merely pointed out a fact and then offered a far simpler soloution, if the end reader choses to use it, its up to them.

konithomimo
10-07-2005, 12:55 PM
I understand that and agree. I am simply giving them some code to use. I didnt say that your method didnt work.

Charles
10-07-2005, 01:04 PM
AND if the number of minutes is less than 10 past the hour, you will end up with negative minutes.No you won't. The JavaScript Date object is smarter than that:<script type="text/javascript">
then = new Date('7 October 2005')
then.setMinutes (then.getMinutes() - 10)
alert (then.toTimeString())
</script>

konithomimo
10-07-2005, 01:13 PM
I say that we shouldn't worry about it Charles. We both know that our original code would work, and here is why webstuff. In both our cases (I will use my names to illustrate) we use the proper calls.

I called:

realTime.setMinutes(realTime.getMinutes() - 10);

notice the realTime.getMinutes() in place of using the variable name m.

If I had used m then it would give me a negative numbers. But using getMinutes keeps it in the proper date format.

Charles
10-07-2005, 01:18 PM
I say that we shouldn't worry about it Charles. We both know that our original code would work, and here is why webstuff. In both our cases (I will use my names to illustrate) we use the proper calls.

I called:

realTime.setMinutes(realTime.getMinutes() - 10);

notice the realTime.getMinutes() in place of using the variable name m.

If I had used m then it would give me a negative numbers. But using getMinutes keeps it in the proper date format.That's not why it works. It works because of the way the JavaScript Date object recovers from errors. September 31st is understood to be October 1st. -10 o'clock is understood to be 9:50.

felgall
10-07-2005, 05:11 PM
<script type="text/javascript">
then = new Date()
then.setMinutes (then.getMinutes() - 10)
alert (then.toTimeString())
</script>


This is the simplest code to use to achieve what you want. Date objects know how to adjust the hour, day etc when minutes goes below zero so everything will happen automatically without having to reinvent the wheel.