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