Click to See Complete Forum and Search --> : time
zuzupus
07-07-2003, 07:38 AM
Hi,
is anybody knows how i can get time(system),when i clicks on button(now)
<td>
<select name="time" size="1">
<option value="08:00>08:00</option>
<option value="09:00>09:00</option>
<option value="10:00>10:00</option>
<input type="button" size="1" name="now">
</select>
</td>
is it possible with javascript that when i click this button i will get system time in this select field
thanks in advance
Try something like this:
<html>
<head>
<script type="text/javascript">
function insertTime() {
time = new Date();
var anOption = document.createElement("option")
document.myform.time.options.add(anOption)
anOption.innerText = time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
anOption.Value = time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
}
</script>
</head>
<body>
<form name="myform">
<select name="time" size="1">
<option value="08:00">08:00</option>
<option value="09:00">09:00</option>
<option value="10:00">10:00</option>
</select>
<input type="button" name="now" value="Insert" onclick="insertTime();">
</form>
</body>
</html>
zuzupus
07-07-2003, 08:29 AM
thanks its awesome really very cool now when i click on insert its adding exactly below 10:00 i mean to say
08:00
09:00
10:00
hh:mm //when i click on insert here i am getting as system time as fourth element is it possible that i can get as first element.
hh:mm //first element
08:00
09:00
10:00
thanks in advance
Replace the old function with this new one:
function insertTime() {
time = new Date();
var anOption = document.createElement("option")
document.myform.time.options.add(anOption, 0)
anOption.innerText = time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
anOption.Value = time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
document.myform.time.options.selectedIndex = 0;
}
zuzupus
07-07-2003, 09:10 AM
thanks you very much is it possible then when clciking on this insert the time will add on this select when the time changes otherwise not for eg. let say my system time is 16:12 and when i click on insert button twice it add 2 times on select i want the time add only when time become 16:13
its really appreciable
<script type="text/javascript">
x = 0;
function insertTime() {
time = new Date();
if (x == 0) {
anOption = document.createElement("option")
document.myform.time.options.add(anOption, 0)
anOption.innerText = time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
anOption.Value = time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
x = 1;
}
else {
document.myform.time.options[0].innerText = time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
document.myform.time.options[0].Value = time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
}
document.myform.time.options.selectedIndex = 0;
}
</script>
zuzupus
07-07-2003, 09:32 AM
this one is pretty cool thanks for awesome script well i created 2 select boxes with same script above used which showing the time i want when i clcik on insert button it will calculate the difference of both times form select fields and dispaly on below text name diff
<input size="2" type="text" value="0" name="diff" readonly>
thanks in advance
Please post what you currently have...
zuzupus
07-07-2003, 10:14 AM
<form name="myform">
<td>
<select name="from" size="1">
<option value="08:00">08:00</option>
<option value="09:00">09:00</option>
<option value="10:00">10:00</option>
</select>
</td>
<td>
<select name="time" size="1" onChange="calculateDiff()">
<option value="08:00">08:00</option>
<option value="09:00">09:00</option>
<option value="10:00">10:00</option>
</select>
<input type="button" name="now" value="Insert" onclick="insertTime(); calculateDiff()">
</td>
<td>
<input size="2" type="text" value="0" name="diff" readonly></td>//here im getting NaN:NaN when i use below script
</form>
<script type="text/javascript">
function calculateDiff()
{
var timeFrom = new Date();
var time = new Date();
var timeDiff = new Date();
var time =document.forms["myform"].elements["from"].value.split(":");
timeFrom.setHours(time[0]);
timeFrom.setMinutes(time[1]);
var time =document.forms["myform"].elements["time"].value.split(":");
time.setHours(time[0]);
time.setMinutes(time[1]);
timeDiff.setTime(time.getTime() - timeFrom.getTime());
document.forms["myform"].elements["diff"].value =timeDiff.getHours()-1 + ":" + timeDiff.getMinutes();
}
</script>
unfortunately im getting NaN but when i use onChange on time field then its working but i can get difference something like 2:3 instead of 02:03 i.e. 2 hours & 03 min former suggests 30 min which is wrong
i will be very glad if you help to solve both cases
thanks ina dvance
Try something like this to pad it with 0's...
<script type="text/javascript">
x = 0;
function insertTime() {
time = new Date();
hour = time.getHours();
minute = time.getMinutes();
second = time.getSeconds();
hour = time.getHours().toString().length < 2 ? "0"+hour : "0"+hour;
minute = minute.toString().length < 2 ? "0"+minute : minute;
second = second.toString().length < 2 ? "0"+second : second;
if (x == 0) {
anOption = document.createElement("option")
document.myform.time.options.add(anOption, 0)
anOption.innerText = hour+":"+minute+":"+second;
anOption.Value = hour+":"+minute+":"+second;
x = 1;
}
else {
document.myform.time.options[0].innerText = hour+":"+minute+":"+second;
document.myform.time.options[0].Value = hour+":"+minute+":"+second;
}
document.myform.time.options.selectedIndex = 0;
}
</script>
zuzupus
07-08-2003, 06:32 AM
sorry but still im getting NaN:NaN as well as the time now im getting soemthinglike 013:33
i am attaching the file then may be u wil coem to know what exactly i want when i click on insert button sysem time as well as it will show tiem diff, in ist field
thanks in advance