divya said:
Actually I want to compare two times which i am takng from the four
comboboxes two for IN Time(hour and Minute) and other two for Out
time(hour and minute) ,if user adds in time as 18:30 and out time as
12:40 ,then I hav to prompt him that out time can't be before in time
for the same day.
How does the time object know it's the same day? Sounds to me like you
need to check out the Date object.
I thought of using Timeserial for combining the selected values of the
hr and minute comboxes into one Time variable. And then Compare the two
times I get for the In and Out.Aneway I used another method for
checking the In time and out time constraint.
Can we have variables which can hold time values in javascript the way
we have in VBscript?
Not built-in, but you could make your own Timeserial object, here's an
example (it needs more work, hopefullly you get the idea):
<script type="text/javascript">
// Constructor: pass either hr, min or 'hr:min'
function Timeserial(hr, min){
if ('string' == typeof hr){
var t = hr.split(':');
if (2 == t.length) {
hr = t[0];
min = t[1];
}
}
this.setTime(hr, min);
}
Timeserial.prototype.addZ = function(n){
return (n<10)?'0'+n : '' + n;
}
// Set the time given hours and minutes
Timeserial.prototype.setTime = function(hr, min){
hr = hr || 0;
min = min || 0;
min = hr*60 + +min;
this.hr = (min/60) | 0;
this.min = min % 60;
}
// Return value of Timeserial as a string hh:mm
Timeserial.prototype.toTimeString = function(){
return this.addZ(this.hr) + ':' + this.addZ(this.min);
}
// Return value in minutes
Timeserial.prototype.toMin = function(){
return this.addZ(this.hr*60 + this.min);
}
// Add hr and min to a Timeserial object
Timeserial.prototype.add = function(hr, min){
this.setTime(+hr + this.hr, +min + this.min);
return this;
}
// Return a time string that is t0 + t1 as hh:mm
// where t0 & t1 are Timeserial objects
Timeserial.prototype.addTimeserials = function(t0, t1){
var t2 = new Timeserial(0, +t0.toMin() + +t1.toMin());
return t2.toTimeString();
}
// Tests
var tm0 = new Timeserial(5, 46); // 05:46
var tm1 = new Timeserial('18:75'); // 19:15
alert(
'tm0: ' + tm0.toTimeString()
+ '\n' + 'tm1: ' + tm1.toTimeString()
+ '\n' + 'tm0 + tm1: ' + tm0.addTimeserials(tm0, tm1)
+ '\n' + 'tm0 + 00:45: ' + (tm0.add(0,45)).toTimeString()
+ '\n' + 'tm0 - 00:45: ' + (tm0.add(0,-45)).toTimeString()
);
</script>