Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > parseInt()

Reply
Thread Tools

parseInt()

 
 
jacster
Guest
Posts: n/a
 
      06-06-2005
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.

 
Reply With Quote
 
 
 
 
LV_Indy
Guest
Posts: n/a
 
      06-06-2005
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);
}
}

 
Reply With Quote
 
 
 
 
jacster
Guest
Posts: n/a
 
      06-06-2005
Thanks!
I'll write that check in.

 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      06-06-2005
JRS: In article <(E-Mail Removed). com>,
dated Mon, 6 Jun 2005 10:22:22, seen in news:comp.lang.javascript,
jacster <(E-Mail Removed)> 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.
 
Reply With Quote
 
jacster
Guest
Posts: n/a
 
      06-06-2005
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);
> }
> }


 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      06-06-2005
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
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      06-06-2005
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?
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      06-06-2005
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
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      06-07-2005
JRS: In article <(E-Mail Removed). com>,
dated Mon, 6 Jun 2005 13:16:17, seen in news:comp.lang.javascript,
jacster <(E-Mail Removed)> 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.
 
Reply With Quote
 
jacster
Guest
Posts: n/a
 
      06-08-2005
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.

 
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




Advertisments