Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-09-2003, 11:04 AM   PM User | #1
zuzupus
Regular Coder

 
Join Date: Jun 2003
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
zuzupus is an unknown quantity at this point
NaN problem

Hi,

iam using a script which is mixture of javascript,HTMl and DHTMl,the latter one used specially to get system when clikced on button called Insert and in same time i will get the difference between to select fields.
Code:
<form name=tstest>
<td>
<select name="von" size="1">
<option value="08:00">08:00</option>
<option value="09:00">09:00</option>
<option value="10:40">10:40</option>
</select>
</td>
<td>
<select name="bis" size="1" onChange="calculateDiff()">
<option value="08:00">08:00</option>
<option value="09:00">09:00</option>
<option value="12:10">12:10</option>
</select>
<input type="button" name="now" value="Insert" onclick="insertTime();calculateDiff()">
</td>
<td>
<input size="2" type="text" value="0" name="ist" readonly></td>
</form>
 
<script type="text/javascript">
function calculateDiff()
{
var time =document.forms["tstest"].elements["von"].value.split(":");
   totime=parseInt(time[0]*60)+parseInt(time[1]);
    var time1 =document.forms["tstest"].elements["bis"].value.split(":");
    time2=parseInt(time1[0]*60) +parseInt(time1[1])
      diff=(time2-totime)
      min=(diff%60)
       hrs=(diff-min)/60
   if (hrs<=9)
   hrs="0"+hrs
   if(min<=9)
   min="0"+min
    document.forms["tstest"].elements["ist"].value =hrs+":"+min;
}
</script>
 
 //DHTML used to get system time in bis field

<script type="text/javascript">
x = 0;
function insertTime() {
	time = new Date();
	hour = time.getHours();
	minute = time.getMinutes();
	hour = time.getHours().toString().length < 2 ? "0"+hour : hour;
	minute = minute.toString().length < 2 ? "0"+minute : minute;
	if (x == 0) {
	anOption = document.createElement("option")
	document.tstest.bis.options.add(anOption, 0)
	anOption.innerText = hour+":"+minute
	anOption.Value = hour+":"+minute
	x = 1;
}
	else {
document.tstest.bis.options[0].innerText = hour+":"+minute
document.tstest.bis.options[0].Value = hour+":"+minute
}
document.tstest.bis.options.selectedIndex = 0;
}
</script>
 

if anybody sort out the problem will really be appreciable

Last edited by zuzupus; 07-09-2003 at 11:08 AM..
zuzupus is offline   Reply With Quote
Old 07-09-2003, 11:44 AM   PM User | #2
ellisd5
Regular Coder

 
ellisd5's Avatar
 
Join Date: Jun 2002
Location: Uk
Posts: 160
Thanks: 5
Thanked 0 Times in 0 Posts
ellisd5 is an unknown quantity at this point
Im not 100% sure but I think its to do with your parseInt

if your string is "08" or "09" which it is in your code parseInt returns a 0.

And its not mathematicly possible to times a number by 0.

The sollution is to use parseInt(yourString, 10) and this will convert the String into the number format you want.

Theres a post on here that explains why this happens as I had the same problem.

http://www.codingforums.com/showthre...threadid=22711
ellisd5 is offline   Reply With Quote
Old 07-09-2003, 12:25 PM   PM User | #3
zuzupus
Regular Coder

 
Join Date: Jun 2003
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
zuzupus is an unknown quantity at this point
sorry its not working but if i use
<select name="bis" size="1" onChange="calculateDiff()">
then its calculating the difference perfectly without using parseInt(mySting,10),even i used this no change in result but why its not working when insertfunction() is called it will diplay system time on display and when time comes to calculate diff. its still showing NaN:NaN

if possible just give a glimpse on DHTML,i think some problem there but where dont no
zuzupus is offline   Reply With Quote
Old 07-09-2003, 12:46 PM   PM User | #4
ellisd5
Regular Coder

 
ellisd5's Avatar
 
Join Date: Jun 2002
Location: Uk
Posts: 160
Thanks: 5
Thanked 0 Times in 0 Posts
ellisd5 is an unknown quantity at this point
Ok this works for me, Something funny was going on with the option you were creating, so i recoded that part of it.

Code:
	<script type="text/javascript">
		function calculateDiff() {
			var time =document.forms["tstest"].elements["von"].value.split(":");
		   	totime=parseInt(time[0]*60)+parseInt(time[1]);
			var time1 =document.forms["tstest"].elements["bis"].value.split(":");
			time2=parseInt(time1[0]*60) +parseInt(time1[1])
			diff=(time2-totime)
			min=(diff%60)
			hrs=(diff-min)/60
		   	if (hrs<=9)
		   		hrs="0"+hrs
		   	if(min<=9)
		   		min="0"+min
			document.forms["tstest"].elements["ist"].value =hrs+":"+min;
		}

		var x = 0;
		function insertTime() {
			time = new Date();
			hour = time.getHours();
			minute = time.getMinutes();
			hour = time.getHours().toString().length < 2 ? "0"+hour : hour;
			minute = minute.toString().length < 2 ? "0"+minute : minute;
			if (x==1) {
				document.tstest.bis.options[document.tstest.bis.options.length-1] = null;
			}
			var anOption = new Option(hour+":"+minute, hour+":"+minute);
			document.tstest.bis.options[document.tstest.bis.options.length] = anOption
			x = 1;
			document.tstest.bis.options.selectedIndex = document.tstest.bis.options.length-1;
		}
	</script>
ellisd5 is offline   Reply With Quote
Old 07-09-2003, 01:30 PM   PM User | #5
zuzupus
Regular Coder

 
Join Date: Jun 2003
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
zuzupus is an unknown quantity at this point
its awesome realyy gr8!! but i got slight problem when i select 12:10 from 'bis' and '10:40' from von then difference between two fields coming 01:30 instead of 01:70,but for rest difference specially having zero min (09:00-08:00)its calculating almost fine,but when something like (14:32-10:40) it coming 03:52 instead of 03:92.

hope you understood,thanks alot for your nice script
zuzupus is offline   Reply With Quote
Old 07-09-2003, 02:08 PM   PM User | #6
ellisd5
Regular Coder

 
ellisd5's Avatar
 
Join Date: Jun 2002
Location: Uk
Posts: 160
Thanks: 5
Thanked 0 Times in 0 Posts
ellisd5 is an unknown quantity at this point
I dont understand I though you wanted the time differenece between the two times

And you coded that correctly
ellisd5 is offline   Reply With Quote
Old 07-09-2003, 02:22 PM   PM User | #7
zuzupus
Regular Coder

 
Join Date: Jun 2003
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
zuzupus is an unknown quantity at this point
ok no problem,,,,thansk u very much i really appreciate you for figure out the fault in script
zuzupus is offline   Reply With Quote
Old 07-11-2003, 09:47 AM   PM User | #8
zuzupus
Regular Coder

 
Join Date: Jun 2003
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
zuzupus is an unknown quantity at this point
is it possible to add system time as first element as this script always add at last element

i want something like this
10:49// system time
08:00
09:00
10:00

but now its adding like
08:00
09:00
10:00
10:49// system time

and is it possible to that if system time shows 08:30 then it will add between 08 and 09 or if it shows 09:30 it will show between 9 and 10,if this one is possible then it will be gr8

thanks in advance
zuzupus is offline   Reply With Quote
Old 07-11-2003, 10:07 AM   PM User | #9
ellisd5
Regular Coder

 
ellisd5's Avatar
 
Join Date: Jun 2002
Location: Uk
Posts: 160
Thanks: 5
Thanked 0 Times in 0 Posts
ellisd5 is an unknown quantity at this point
This code will round the number to nearers half hour.
Havent solved the making it go at the top problem.
What i have done in the past is to create an array of the options. Then when its time to add the new option, i remove all of them and put them back in the order i want.

There must be a better way of doing it, does anyone know?
The company i work for have to surport IE 5.5 and NS 4.6 so if theres a piece of code that works for both would be good for me as well.

Code:
	<script type="text/javascript">
		function calculateDiff() {
			var time =document.forms["tstest"].elements["von"].value.split(":");
		   	totime=parseInt(time[0]*60)+parseInt(time[1]);
			var time1 =document.forms["tstest"].elements["bis"].value.split(":");
			time2=parseInt(time1[0]*60) +parseInt(time1[1])
			diff=(time2-totime)
			min=(diff%60)
			hrs=(diff-min)/60
		   	if (hrs<=9)
		   		hrs="0"+hrs
		   	if(min<=9)
		   		min="0"+min
			document.forms["tstest"].elements["ist"].value =hrs+":"+min;
		}

		var x = 0;
		function insertTime() {
			time = new Date();
			hour = time.getHours();
			minute = time.getMinutes();
			if (minute < 15) { minute="00"; }
			else if (minute>=15 && minute < 45) { minute="30"; }
			else { minute="00"; hour++; }
			hour = time.getHours().toString().length < 2 ? "0"+hour : hour;
			if (x==1) {
				document.tstest.bis.options[document.tstest.bis.options.length-1] = null;
			}
			var anOption = new Option(hour+":"+minute, hour+":"+minute);
			document.tstest.bis.options[0] = anOption
			x = 1;
			document.tstest.bis.options.selectedIndex = document.tstest.bis.options.length-1;
		}
	</script>
ellisd5 is offline   Reply With Quote
Old 07-11-2003, 10:10 AM   PM User | #10
ellisd5
Regular Coder

 
ellisd5's Avatar
 
Join Date: Jun 2002
Location: Uk
Posts: 160
Thanks: 5
Thanked 0 Times in 0 Posts
ellisd5 is an unknown quantity at this point
made an error:

Code:
document.tstest.bis.options[i] = anOption
should be

Code:
document.tstest.bis.options[document.tstest.bis.length] = anOption
ellisd5 is offline   Reply With Quote
Old 07-11-2003, 10:23 AM   PM User | #11
zuzupus
Regular Coder

 
Join Date: Jun 2003
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
zuzupus is an unknown quantity at this point
sorry still its adding at the end not as first element ,is it not possible that whatever the system time it will show between corresponding
lat say if sytem time is 11:26 then it will show between 11:25 and 11:30

thanks in advance
zuzupus is offline   Reply With Quote
Old 07-11-2003, 12:23 PM   PM User | #12
zuzupus
Regular Coder

 
Join Date: Jun 2003
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
zuzupus is an unknown quantity at this point
got it the problem why its not adding up above

regards
zuzupus is offline   Reply With Quote
Old 07-12-2003, 02:10 PM   PM User | #13
zuzupus
Regular Coder

 
Join Date: Jun 2003
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
zuzupus is an unknown quantity at this point
hi hharchester

did u got the solution why its not adding top as i tried unfortunately still struggling and is it possible when user selects time from 'von' as 08:00 then at 'bis' it will show only from 08 to 08:55 then corrsponding time let say 09,10,11 etc. will show 09 to 09:55,10 to 10:55----


hope this is clear to you-----


thanks and looking forward to reply by anyone
zuzupus is offline   Reply With Quote
Old 07-14-2003, 10:38 AM   PM User | #14
ellisd5
Regular Coder

 
ellisd5's Avatar
 
Join Date: Jun 2002
Location: Uk
Posts: 160
Thanks: 5
Thanked 0 Times in 0 Posts
ellisd5 is an unknown quantity at this point
I have coded it to add the element to the bottom,
I would remove all the options and add them again in the order you wanted (With the newly created one at the top).

I was hoping someone else could provide a better way of doing this as i do not not another way.
ellisd5 is offline   Reply With Quote
Old 07-14-2003, 01:23 PM   PM User | #15
zuzupus
Regular Coder

 
Join Date: Jun 2003
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
zuzupus is an unknown quantity at this point
did u find the solution to add at top even i didnt find any solution for this...hope you or anybody will let me know

thanks
zuzupus is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:04 PM.


Advertisement
Log in to turn off these ads.