parseInt()

Discussion in 'Javascript' started by jacster, Jun 6, 2005.

  1. jacster

    jacster Guest

    Hi,
    I'm trying to parse a string of the form 08:00 representing a time so I
    can calculate the difference between two times.

    parseInt(time) with a leading zero returns 0.

    Is there a way around this without writing a routine to check for the
    zero first?

    Thanks,
    Malcolm.
    jacster, Jun 6, 2005
    #1
    1. Advertising

  2. jacster

    LV_Indy Guest

    Not that I know of. parseInt and toString() are pretty dump functions
    (as in they don't have customization switches or anything, they just do
    exactly as advertised). It would be a really easy routine to write
    though.

    function parseTime(time) {
    if (time.indexOf('0') == 0) {
    return parseInt(time.substr(1));
    } else {
    return parseInt(time);
    }
    }
    LV_Indy, Jun 6, 2005
    #2
    1. Advertising

  3. jacster

    jacster Guest

    Thanks!
    I'll write that check in.
    jacster, Jun 6, 2005
    #3
  4. JRS: In article <>,
    dated Mon, 6 Jun 2005 10:22:22, seen in news:comp.lang.javascript,
    jacster <> posted :

    >I'm trying to parse a string of the form 08:00 representing a time so I
    >can calculate the difference between two times.
    >
    >parseInt(time) with a leading zero returns 0.
    >
    >Is there a way around this without writing a routine to check for the
    >zero first?


    Yes; you should read the FAQ of this newsgroup, which explains how you
    should use parseInt for such numbers.

    But you do not need parseInt; if you check the format with a RegExp you
    can at the same time extract the desired substrings (see via below) or
    you can use something like

    T = F.X0.value.split(':')
    h = +T[0] ; m = +T[1] ; M = h*60+m


    Note FAQ 4,21, as well.

    --
    © 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, Jun 6, 2005
    #4
  5. jacster

    jacster Guest

    Okay,
    Can anyone tell me what's wrong with this procedure..

    /*
    function split_time(time) {
    if (time.substr(0,0) == 0) {
    hours = parseInt(time.substr(1,1));
    }
    else {
    hours = parseInt(time.split(":")[0]);
    }
    if (time.substr(3,3) == 0) {
    mins = parseInt(time.substr(4,4));
    }
    else {
    mins = parseInt(time.split(":")[1]);
    }
    num_time = [hours, mins];
    return num_time;
    }
    alert(split_time("02:09"));
    */

    For some reason, it works except when time.substr(4,4) < 8

    LV_Indy wrote:
    > Not that I know of. parseInt and toString() are pretty dump functions
    > (as in they don't have customization switches or anything, they just do
    > exactly as advertised). It would be a really easy routine to write
    > though.
    >
    > function parseTime(time) {
    > if (time.indexOf('0') == 0) {
    > return parseInt(time.substr(1));
    > } else {
    > return parseInt(time);
    > }
    > }
    jacster, Jun 6, 2005
    #5
  6. jacster

    Randy Webb Guest

    jacster wrote:
    > Hi,
    > I'm trying to parse a string of the form 08:00 representing a time so I
    > can calculate the difference between two times.
    >
    > parseInt(time) with a leading zero returns 0.
    >
    > Is there a way around this without writing a routine to check for the
    > zero first?


    Yes, you use parseInt with it's intended radix. Had you read this groups
    FAQ you would have found this very question answered. The faq can be
    found in my signature, the specific section you are hunting is the
    section that asks "Why does parseInt('09') give an error?"

    For the unitiated, it is at http://jibbering.com/faq/#FAQ4_12

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Randy Webb, Jun 6, 2005
    #6
  7. jacster

    Randy Webb Guest

    LV_Indy wrote:

    > Not that I know of. parseInt and toString() are pretty dump functions
    > (as in they don't have customization switches or anything, they just do
    > exactly as advertised). It would be a really easy routine to write
    > though.
    >
    > function parseTime(time) {
    > if (time.indexOf('0') == 0) {
    > return parseInt(time.substr(1));
    > } else {
    > return parseInt(time);
    > }
    > }



    Or just use a radix. parseInt(time,10).

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Answer:It destroys the order of the conversation
    Question: Why?
    Answer: Top-Posting.
    Question: Whats the most annoying thing on Usenet?
    Randy Webb, Jun 6, 2005
    #7
  8. jacster

    Randy Webb Guest

    jacster wrote:

    > Okay,
    > Can anyone tell me what's wrong with this procedure..


    That depends on what you think the procedure should do.

    > /*
    > function split_time(time) {
    > if (time.substr(0,0) == 0) {
    > hours = parseInt(time.substr(1,1));
    > }
    > else {
    > hours = parseInt(time.split(":")[0]);
    > }


    Dump that code.

    hours = parseInt(time,10);

    > if (time.substr(3,3) == 0) {
    > mins = parseInt(time.substr(4,4));
    > }
    > else {
    > mins = parseInt(time.split(":")[1]);
    > }
    > num_time = [hours, mins];
    > return num_time;
    > }
    > alert(split_time("02:09"));
    > */



    On second thought, dump it all.

    function split_time(time){
    myArray = time.split(':')
    alert('hour = ' + myArray[0]')
    alert('minutes = ' + myArray[1]')
    alert('seconds = ' + myArray[2]')
    }

    Thats assuming that it goes into the function the hh:mm:ss format.

    split_time('10:30:45')

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Randy Webb, Jun 6, 2005
    #8
  9. JRS: In article <>,
    dated Mon, 6 Jun 2005 13:16:17, seen in news:comp.lang.javascript,
    jacster <> posted :
    >Okay,
    >Can anyone tell me what's wrong with this procedure..
    >
    >/*
    >function split_time(time) {
    >if (time.substr(0,0) == 0) {
    >hours = parseInt(time.substr(1,1));
    >}
    >else {
    >hours = parseInt(time.split(":")[0]);
    >}
    >if (time.substr(3,3) == 0) {
    >mins = parseInt(time.substr(4,4));
    >}
    >else {
    >mins = parseInt(time.split(":")[1]);
    >}
    >num_time = [hours, mins];
    >return num_time;
    >}
    >alert(split_time("02:09"));
    >*/
    >
    >For some reason, it works except when time.substr(4,4) < 8


    Since it is in comment, it has neither error nor utility.
    It's too long.

    You seem to be using substr as if it were substring, though without
    adverse effect.

    Code should be indented to show intended structure.

    You're misusing parseInt; see FAQ.

    IMHO, parseInt should only be used of the intended base is not 10, or if
    the string has non-numeric trailing characters. Normally, unary + is
    better.

    var T = time.split(":")
    // alert([+T[0], +T[1]])
    MINS = T[0]*60 + +T[1]

    --
    © 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, Jun 7, 2005
    #9
  10. jacster

    jacster Guest

    Hi,
    The code is too long. The comments were intentional. I originally had
    what Randy suggested but for some reason further processing I used
    needed a type conversion which is why I started with parse int.

    What I want is a function that takes two string parameters of the form
    hh:mm in 24h format and calculates the difference. I've been doing
    other things, though and'll give it another go after reading the FAQ as
    well as consider doing the calculation in millisecond format.

    Thanks.
    jacster, Jun 9, 2005
    #10
  11. jacster

    Jc Guest

    > What I want is a function that takes two string parameters of the form
    > hh:mm in 24h format and calculates the difference.


    You really don't have to convert the time parts into numbers if you are
    doing a subtraction operation on them. Javascript will automatically
    convert the strings into numbers for you (type coercion), since the "-"
    operator doesn't apply to strings. Of course, if you later changed the
    code to use the "+" operator you would run into unexpected results
    since the numbers would be concatenated rather than added, so having a
    parseInt(sNum, 10) for clarity isn't a bad idea.

    I'm not sure why you are only working with hours and minutes, I can
    only assume you are somehow guaranteed that they will always be from
    the same day. Depending on how robust this code needs to be, you may
    also want to take into consideration the effects of daylight savings
    time. If the two times being subtracted include a DST change, the
    results may not be what you expect to see.

    The JavaScript Date object applies to the client computer, and so using
    the Date object and specifying the full date and subtracting Date
    objects would be one approach to handling the DST issue. Documentation
    for the Date object can be found at
    http://msdn.microsoft.com/library/en-us/script56/html/js56jsobjdate.asp.

    The example below uses the HH:SS format as you specified, although it
    could be modified to work with a full date format:

    function getHourMinDifInMinutes(sHourMin1, sHourMin2) {
    var dt1 = getHourMinAsDate(sHourMin1);
    var dt2 = getHourMinAsDate(sHourMin2);
    var iDif_ms = dt2 - dt1;

    return(iDif_ms / 1000 / 60);
    } //getHourMinDifInMinutes

    function getHourMinAsDate(sHourMin) {
    var aHourMin = sHourMin.split(":");

    //new Date(year, month, date[, hours[, minutes[, seconds[,ms]]]])
    var dt = new Date(1900, 0, 1, aHourMin[0], aHourMin[1]);

    return(dt);
    } //getHourMinAsDate

    alert(getHourMinDifInMinutes("02:09", "03:09"));
    Jc, Jun 9, 2005
    #11
  12. JRS: In article <>
    , dated Wed, 8 Jun 2005 16:33:28, seen in news:comp.lang.javascript,
    jacster <> posted :

    >What I want is a function that takes two string parameters of the form
    >hh:mm in 24h format and calculates the difference. I've been doing
    >other things, though and'll give it another go after reading the FAQ as
    >well as consider doing the calculation in millisecond format.


    If you are *starting* with S1 and S2 of the form 'hh:mm', then

    var T1 = S1.split(":") , T2 = S2.split(":") ,
    MINS = (T2[0]-T1[0])*60 + (T2[1]-T1[1])

    The last pair of parentheses are necessary (well, so are the others).

    --
    © 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, Jun 9, 2005
    #12
  13. jacster wrote:

    > I'm trying to parse a string of the form 08:00 representing a
    > time so I can calculate the difference between two times.
    >
    > parseInt(time) with a leading zero returns 0.


    Because the implementation tested with supports non-standard deprecated
    octal literals (specified with a leading `0' and there is no digit `8'
    in this number system. Only the digit `0' is found appropriate and the
    character string is converted accordingly.

    > Is there a way around this without writing a routine to check for the
    > zero first?


    Use

    parseInt(time, 10)

    to specify that you intend to parse a decimal number.
    Please read the FAQ before posting.


    PointedEars
    --
    [George W.] Bush wollte die Länder überzeugen. Wo hat er überzeugt?
    Er hat bedroht und bedroht und gekauft und gekauft. Und wo trifft er
    sich mit Blair und Aznar? Auf den Azoren! Weil dort die Menschen nicht
    protestieren können. ([R] Prof. Alfred M. de Zayas, Völkerrechtler)
    Thomas 'PointedEars' Lahn, Jun 16, 2005
    #13
  14. JRS: In article <>, dated Thu, 16 Jun
    2005 20:52:05, seen in news:comp.lang.javascript, Thomas 'PointedEars'
    Lahn <> posted :

    >Use
    >
    > parseInt(time, 10)
    >
    >to specify that you intend to parse a decimal number.
    >Please read the FAQ before posting.


    There is no need to use parseInt(S, 10) under the circumstances.

    Please read the FAQ before posting.
    Please read the whole of the visible thread before posting.

    --
    © 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, Jun 17, 2005
    #14
  15. Dr John Stockton wrote:

    > [...] Thomas 'PointedEars' Lahn [...] posted :
    >>Use
    >>
    >> parseInt(time, 10)
    >>
    >>to specify that you intend to parse a decimal number.
    >>Please read the FAQ before posting.

    >
    > There is no need to use parseInt(S, 10) under the circumstances.


    1. I responded to and explained the OP's problem; why it occurs and how it
    can be avoided.

    2. There is no *need* to use not downwards compatible
    String.prototype.split(), as you did, either, and not downwards
    compatible RegExp objects will also serve the purpose. What's your
    point? Often there is not *the* single solution to a problem, and
    it remains to be discussed which is more and most efficient and,
    apart from that, more advisable.

    > Please read the FAQ before posting.


    BTDT. You could point me directly to the FAQ entry that I missed.

    > Please read the whole of the visible thread before posting.


    BTDT. It was the whole visible thread at that time.


    PointedEars
    Thomas 'PointedEars' Lahn, Jul 3, 2005
    #15
    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. Sam Iam
    Replies:
    6
    Views:
    3,981
    satdmail
    Jul 19, 2006
  2. Knute Johnson

    Interesting Integer.parseInt() problem

    Knute Johnson, Jan 31, 2006, in forum: Java
    Replies:
    4
    Views:
    12,462
    Roedy Green
    Jan 31, 2006
  3. Paul Rubin
    Replies:
    3
    Views:
    702
    jose isaias cabrera
    Feb 1, 2005
  4. Bill Mill
    Replies:
    0
    Views:
    362
    Bill Mill
    Feb 1, 2005
  5. jose isaias cabrera

    Re: Java Integer.ParseInt translation to python

    jose isaias cabrera, Feb 1, 2005, in forum: Python
    Replies:
    0
    Views:
    570
    jose isaias cabrera
    Feb 1, 2005
Loading...

Share This Page