What is an equivalent TimeSerial() function of VBscript in the Java Script??

D

divya

What is an equivalent TimeSerial() function of VBscript in the Java
Script??
I have two variables hour and minutes.
Timeserial(hour,minutes) returns the time hour : minutes.
But I want to do similar thing using the javascript .Can anybody help
me out with the procedure.

Divya.
 
R

Randy Webb

divya said the following on 9/21/2006 1:52 AM:
What is an equivalent TimeSerial() function of VBscript in the Java
Script??
I have two variables hour and minutes.
Timeserial(hour,minutes) returns the time hour : minutes.
But I want to do similar thing using the javascript .Can anybody help
me out with the procedure.

There isn't a built-in one, but, you can write your own very simply:

function Timeserial(hour,minutes){return (hour + ":" + minutes)}
 
R

RobG

divya said:
What is an equivalent TimeSerial() function of VBscript in the Java
Script??
I have two variables hour and minutes.
Timeserial(hour,minutes) returns the time hour : minutes.
But I want to do similar thing using the javascript .Can anybody help
me out with the procedure.

If you state exactly what your requirements are, then maybe.
JavaScript doesn't have a Time object, it has a Date object that might
suit. I guess the trivial solution is:

function timeSerial(hr, min){
function addZ(num){return (num<10)? '0'+num : ''+num;}
return addZ(hr) + ':' + addZ(min);
}

alert(timeSerial('12','2'));
 
D

divya

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.
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?
Ex
tm=Timeserial(12,30)
here variable tm holds time on which I can perform operations like
comparing,adding time...
Thanyou for your suggestions .
Divya.
 
R

RobG

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

Dr John Stockton

JRS: In article <[email protected]>, dated
Thu, 21 Sep 2006 02:38:24 remote, seen in
Randy Webb said:
divya said the following on 9/21/2006 1:52 AM:

It does not. It returns a number of type CDate with value between 0.0
and about 0.999. The default conversion to string of a CDate of that
range gives hh:mm:ss. It is important to understand that distinction,
and the corresponding one in javascript, since otherwise you will
confuse not only yourself.

There isn't a built-in one, but, you can write your own very simply:

function Timeserial(hour,minutes){return (hour + ":" + minutes)}

No, you missed both the need for two Leading Zero operations and, for
fuller equivalence, consideration of what happens if Minutes is not in
the range 0..59 or hours not in 0..23. See my vb-dates.htm ff. pages.


The OP needs to know that VBS CDates are fundamentally in the local
timezone (as days from 1899-12-30.0), whereas JS Date Object stores UTC
(as ms from 1970.0) but uses local time for I/O unless using UTC
methods.

If JS's handling of offset from UTC is ignored, code may be in error
either immediately or when an interval happens to cross a clock
transition.


An *equivalent* function would use either
new Date( 2000, 0, 1, hour, minutes )/864e5 + K1
or new Date(Date.UTC((2000, 0, 1, hour, minutes))/864e5 + K2
but full equivalence is generally not needed.

If hh:mm is required, then one of the JS conversions from Date to String
can be massaged with a RegExp; or your approach can be adapted.

Obviously your function should not be called TimeSerial, since it is too
different from VBS's.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Thu, 21 Sep 2006 01:56:53 remote, seen in
news:comp.lang.javascript 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.

Then, calling them H1 M1 & H2 M2, test by comparing H1*60 + +M1 with
H2*60 + +M2.

You could alternatively compare hours: if H2>H1 pass, if H2<H1 fail,
else compare minutes.

For that, using Date Objects is inefficient.

It's a good idea to read the newsgroup and its FAQ.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top