parseInt()

J

jacster

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.
 
L

LV_Indy

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

Dr John Stockton

JRS: In article <[email protected]>,
dated Mon, 6 Jun 2005 10:22:22, seen in
jacster said:
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.
 
J

jacster

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
 
R

Randy Webb

jacster said:
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
 
R

Randy Webb

LV_Indy said:
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?
 
R

Randy Webb

jacster said:
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')
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Mon, 6 Jun 2005 13:16:17, seen in
jacster said:
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]
 
J

jacster

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.
 
J

Jc

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"));
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Wed, 8 Jun 2005 16:33:28, seen in
jacster said:
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).
 
T

Thomas 'PointedEars' Lahn

jacster said:
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
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Thu, 16 Jun
2005 20:52:05, seen in Thomas 'PointedEars'
Lahn said:
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.
 
T

Thomas 'PointedEars' Lahn

Dr said:
[...] 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
 

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