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

Discussion in 'Javascript' started by divya, Sep 21, 2006.

  1. divya

    divya Guest

    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.
    divya, Sep 21, 2006
    #1
    1. Advertising

  2. divya

    Randy Webb Guest

    Re: What is an equivalent TimeSerial() function of VBscript inthe Java Script??

    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/
    Randy Webb, Sep 21, 2006
    #2
    1. Advertising

  3. divya

    RobG Guest

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

    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
    RobG, Sep 21, 2006
    #3
  4. divya

    divya Guest

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

    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
    divya, Sep 21, 2006
    #4
  5. divya

    RobG Guest

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

    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
    RobG, Sep 21, 2006
    #5
  6. What is an equivalent TimeSerial() function of VBscript in the Java Script??

    JRS: In article <>, dated
    Thu, 21 Sep 2006 02:38:24 remote, seen in news:comp.lang.javascript,
    Randy Webb <> 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.
    Dr John Stockton, Sep 21, 2006
    #6
  7. Re: What is an equivalent TimeSerial() function of VBscript in the Java Script??

    JRS: In article <>,
    dated Thu, 21 Sep 2006 01:56:53 remote, seen in
    news:comp.lang.javascript, divya <> 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.
    Dr John Stockton, Sep 21, 2006
    #7
  8. divya

    divya Guest

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

    Thank you John and Rob for your time and explaination.
    Divya
    Dr John Stockton wrote:
    > JRS: In article <>, dated
    > Thu, 21 Sep 2006 02:38:24 remote, seen in news:comp.lang.javascript,
    > Randy Webb <> 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.
    divya, Sep 25, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Ciotuszynski

    Javascript:return vbscript equivalent

    Jim Ciotuszynski, Jul 6, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    6,593
    nescafe
    Sep 15, 2011
  2. sundar_s1210
    Replies:
    4
    Views:
    42,158
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    May 9, 2005
  3. lonelyplanet999
    Replies:
    8
    Views:
    42,907
  4. Replies:
    3
    Views:
    174
    Matt Kruse
    May 25, 2006
  5. shelleybobelly

    javascript equivalent for vbscript Date()-1

    shelleybobelly, Mar 28, 2007, in forum: Javascript
    Replies:
    18
    Views:
    269
    Dr J R Stockton
    Mar 30, 2007
Loading...

Share This Page