...

View Full Version : NaN problem



zuzupus
07-09-2003, 11:04 AM
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.



<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

ellisd5
07-09-2003, 11:44 AM
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/showthread.php?s=&threadid=22711

zuzupus
07-09-2003, 12:25 PM
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 :(

ellisd5
07-09-2003, 12:46 PM
Ok this works for me, Something funny was going on with the option you were creating, so i recoded that part of it.




<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>

zuzupus
07-09-2003, 01:30 PM
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

ellisd5
07-09-2003, 02:08 PM
I dont understand I though you wanted the time differenece between the two times :confused:

And you coded that correctly

zuzupus
07-09-2003, 02:22 PM
ok no problem,,,,thansk u very much i really appreciate you for figure out the fault in script

zuzupus
07-11-2003, 09:47 AM
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

ellisd5
07-11-2003, 10:07 AM
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.



<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
07-11-2003, 10:10 AM
made an error:



document.tstest.bis.options[i] = anOption


should be



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

zuzupus
07-11-2003, 10:23 AM
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
07-11-2003, 12:23 PM
got it the problem why its not adding up above :)

regards

zuzupus
07-12-2003, 02:10 PM
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

ellisd5
07-14-2003, 10:38 AM
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.

zuzupus
07-14-2003, 01:23 PM
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

ellisd5
07-14-2003, 02:16 PM
This is what i have been describing that i would do to fix your problem.
I have removed all the options then added them back in but also adding the new option first, thus making it go to the top.
Like i said i was hoping someone wud post up an easier sollution than this but i dont look like thats going to happen so heres the code to do it the way i would do it.



<html>
<head>

<script type="text/javascript">

var times = new Array ();
times[0] = new Option("08:00", "08:00");
times[1] = new Option("09:00", "09:00");
times[2] = new Option("12:10", "12:10");

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;
}

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;
// remove all options
while (document.tstest.bis.length > 0) {
document.tstest.bis.options[document.tstest.bis.length-1] = null;
}
var anOption = new Option(hour+":"+minute, hour+":"+minute);
document.tstest.bis.options[document.tstest.bis.length] = anOption
// add orignal options
for (i=0; i<times.length; i++) {
document.tstest.bis.options[document.tstest.bis.length] = times[i];
}
document.tstest.bis.options.selectedIndex = 0;
}
</script>

</head>
<body>

<form name="tstest">
<table>
<tr>
<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>
</td>
</tr>
</table>
</form>

</body>
</html>

zuzupus
07-14-2003, 03:40 PM
excellent! its really appreciable from bottom of heart...
but there is one problem
var times = new Array ();
times[0] = new Option("08:00", "08:00");
times[1] = new Option("09:00", "09:00");
times[2] = new Option("12:10", "12:10");

as im using PHP for getting the time in both select field
<select name="von">
<option selected value="<?=date('H:i')?>"><? echo (date('H:i')); ?></option>
<? fillDD(0, 0, -1); ?>//i used one function to call hr and min starts from 0 to 23 hrs as well as 0 to 23 min somethign like 08:00
</select>

is it possible to avoid above new Option("08:00", "08:00"); and directly in one line i can use soem PHP there

hope you will be clear otherwise i have to har code for time then script beautiness will kill

thansk in advance

ellisd5
07-14-2003, 03:56 PM
Why dont you populate the array the same way you populate the form?

zuzupus
07-14-2003, 04:01 PM
if i populate the form then i have to hard code all the time starting from 0:00 till 23:55 then more 25 lines added i just want to reduce the line as much as possible

<td nowrap align="center">
<select name="von">
<option selected value="<?=date('H:i')?>"><? echo (date('H:i')); ?></option>
<? fillDD(0, 0, -1); ?>
</select>
</td>
<td nowrap align="center">
<select name="bis" >
<option value="<?=date('H:i')?>"><? echo (date('H:i')); ?></option>
<?fillDD(0, 0, -1); ?>
</select>
<input type="button" name="now" size="1" value=">" onclick="insertTime();calculateIst()">
</td>

waiting for ur change script

thanks

zuzupus
07-15-2003, 09:38 AM
hi,
when im using this one then im getting only system time not able to see list from bis

var t = document.tstest.bis.optiondocument.tstest.bis.selectedIndex].value
times= new Option("t", "t");
for (i=0; i<times.length; i++) {
document.tstest.bis.options[document.tstest.bis.length] = times;
}
document.tstest.bis.options.selectedIndex = 0;

could u plz sort out what wrong in thsi script so that i can able to view the bis list

thansk in advance

ellisd5
07-15-2003, 10:12 AM
Eh, I see what your trying to do, good idea needs a bit of a reshuffle though and some of the coding is wrong.




<html>
<head>

<script type="text/javascript">

var times = new Array ();
function createArray() {
for (i=0; i<document.tstest.bis.length; i++) {
times[times.length] = new Option(document.tstest.bis[i].value, document.tstest.bis[i].value);
}
}

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;
}

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;
// remove all options
while (document.tstest.bis.length > 0) {
document.tstest.bis.options[document.tstest.bis.length-1] = null;
}
var anOption = new Option(hour+":"+minute, hour+":"+minute);
document.tstest.bis.options[document.tstest.bis.length] = anOption
// add orignal options
for (i=0; i<times.length; i++) {
document.tstest.bis.options[document.tstest.bis.length] = times[i];
}
document.tstest.bis.options.selectedIndex = 0;
}
</script>

</head>
<body onload="createArray()">

<form name="tstest">
<table>
<tr>
<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>
</td>
</tr>
</table>
</form>

</body>
</html>

zuzupus
07-15-2003, 10:24 AM
awesome thank you very much is it necessary to give onload as createArray() there is no other way to do that


thanks

ellisd5
07-15-2003, 10:27 AM
Why would you not want to use onload?

zuzupus
07-15-2003, 10:29 AM
coz i already have lot of onloads on my body tag :)

ellisd5
07-15-2003, 10:53 AM
Theres no problem calling more than one function onsubmit. Just seperate them with a comma:



<body onload="func1(); func2(); func3();">


or you can create a function to call all these;



function init() {
func1();
func2();
func3();
}
...
...
...
<body onload="init();">

zuzupus
07-15-2003, 11:31 AM
thanks alot :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum