Page 1 of 1

Help with Javascript

Posted: Sat 24 Dec 2011 5:11 am
by MickinMoulden
Can anyone see anything wrong with this?

Code: Select all

<script type="text/javascript">

             var date = new Date();
	var y=date.getFullYear();
	var m=date.getMonth();
	var d=date.getDay();
	var h=date.getHours();
	var min=date.getMinutes();
	if (h<9)
	{var d=d-1;
	var rollover= new Date(y, m, d, 9,0,0,0);
	}else (rollover= new Date(y, m, d, 9,0,0,0));
	var now=new Date();
	var timeelapsed=now-rollover;
             
....more code follows.

Just finding the time difference (in milliseconds) between actual time and the rollover time (for me is 0900hrs).
If nothing is wrong here, then I'll have to check the rest of the code.

Re: Help with Javascript

Posted: Sat 24 Dec 2011 9:20 am
by MickinMoulden
It's all done and fixed.

Re: Help with Javascript

Posted: Sat 24 Dec 2011 10:25 am
by mcrossley
No curly brackets around your else is bad practice, you don't need 'now' as you already have now in 'date', and have you tested when d=1-1? It should work with the Date() object rolling back the month/year automatically, but worth a test of 8am on the first of January.

But don't keep us in suspense, what was the problem?

Re: Help with Javascript

Posted: Sat 24 Dec 2011 10:46 am
by MickinMoulden
MickinMoulden wrote:Can anyone see anything wrong with this?

Code: Select all

<script type="text/javascript">

             var date = new Date();
	var y=date.getFullYear();
	var m=date.getMonth();
	var d=date.getDay();    **getDate**is what was needed, it gives 0-31, **detDay** is 0-6!
	var h=date.getHours();
	var min=date.getMinutes();
	if (h<9)
	{var d=d-1;
	var rollover= new Date(y, m, d, 9,0,0,0);
	}else (rollover= new Date(y, m, d, 9,0,0,0));
	var now=new Date();
	var timeelapsed=now-rollover;
             
....more code follows.

Just finding the time difference (in milliseconds) between actual time and the rollover time (for me is 0900hrs).
If nothing is wrong here, then I'll have to check the rest of the code.

Re: Help with Javascript

Posted: Sat 24 Dec 2011 11:36 am
by mcrossley
Doh! Didn't spot that one!