Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > What is an equivalent TimeSerial() function of VBscript in the Java Script??

Reply
Thread Tools

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

 
 
divya
Guest
Posts: n/a
 
      09-21-2006
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.

 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      09-21-2006
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)}

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      09-21-2006

divya wrote:
> 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'));


--
Rob

 
Reply With Quote
 
divya
Guest
Posts: n/a
 
      09-21-2006
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.

RobG wrote:
> divya wrote:
> > 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'));
>
>
> --
> Rob


 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      09-21-2006

divya wrote:
> 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>


--
Rob

 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      09-21-2006
JRS: In article <(E-Mail Removed)>, dated
Thu, 21 Sep 2006 02:38:24 remote, seen in news:comp.lang.javascript,
Randy Webb <(E-Mail Removed)> posted :
>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.


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.


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


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.

--
John Stockton, Surrey, UK. ???@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      09-21-2006
JRS: In article <(E-Mail Removed) om>,
dated Thu, 21 Sep 2006 01:56:53 remote, seen in
news:comp.lang.javascript, divya <(E-Mail Removed)> posted :

>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.
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
divya
Guest
Posts: n/a
 
      09-25-2006
Thank you John and Rob for your time and explaination.
Divya
Dr John Stockton wrote:
> JRS: In article <(E-Mail Removed)>, dated
> Thu, 21 Sep 2006 02:38:24 remote, seen in news:comp.lang.javascript,
> Randy Webb <(E-Mail Removed)> posted :
> >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.

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

>
> 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.
>
> --
> John Stockton, Surrey, UK. ???@merlyn.demon.co.uk Turnpike v4.00 MIME.
> Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
> Check boilerplate spelling -- error is a public sign of incompetence.
> Never fully trust an article from a poster who gives no full real name.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript:return vbscript equivalent Jim Ciotuszynski ASP .Net 7 09-15-2011 02:19 PM
javascript equivalent for vbscript Date()-1 shelleybobelly Javascript 18 03-30-2007 10:46 PM
Javascript equivalent for VBScript polychrom@softhome.net Javascript 3 05-25-2006 07:53 PM
whats the equivalent of vbcrlf,vbcr,vblf (vbscript) in c#.net sundar_s1210 ASP .Net 4 05-09-2005 04:54 PM
java equivalent function of c atoi() function lonelyplanet999 Java 8 11-03-2003 01:07 AM



Advertisments