Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: NaN problem

  1. #1
    Regular Coder
    Join Date
    Jun 2003
    Posts
    183
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder ellisd5's Avatar
    Join Date
    Jun 2002
    Location
    Uk
    Posts
    160
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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

  • #3
    Regular Coder
    Join Date
    Jun 2003
    Posts
    183
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Regular Coder ellisd5's Avatar
    Join Date
    Jun 2002
    Location
    Uk
    Posts
    160
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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>

  • #5
    Regular Coder
    Join Date
    Jun 2003
    Posts
    183
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Regular Coder ellisd5's Avatar
    Join Date
    Jun 2002
    Location
    Uk
    Posts
    160
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I dont understand I though you wanted the time differenece between the two times

    And you coded that correctly

  • #7
    Regular Coder
    Join Date
    Jun 2003
    Posts
    183
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok no problem,,,,thansk u very much i really appreciate you for figure out the fault in script

  • #8
    Regular Coder
    Join Date
    Jun 2003
    Posts
    183
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #9
    Regular Coder ellisd5's Avatar
    Join Date
    Jun 2002
    Location
    Uk
    Posts
    160
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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>

  • #10
    Regular Coder ellisd5's Avatar
    Join Date
    Jun 2002
    Location
    Uk
    Posts
    160
    Thanks
    5
    Thanked 0 Times in 0 Posts
    made an error:

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

    Code:
    document.tstest.bis.options[document.tstest.bis.length] = anOption

  • #11
    Regular Coder
    Join Date
    Jun 2003
    Posts
    183
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #12
    Regular Coder
    Join Date
    Jun 2003
    Posts
    183
    Thanks
    0
    Thanked 0 Times in 0 Posts
    got it the problem why its not adding up above

    regards

  • #13
    Regular Coder
    Join Date
    Jun 2003
    Posts
    183
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #14
    Regular Coder ellisd5's Avatar
    Join Date
    Jun 2002
    Location
    Uk
    Posts
    160
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

  • #15
    Regular Coder
    Join Date
    Jun 2003
    Posts
    183
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •