parseInt not returning expected value


I am converting seconds to years and months as follows:

var seconds = 1;
var years = parseInt((((seconds / 60) / 60) / 24) / 365);
var months = parseInt((((seconds / 60) / 60) / 24) / 30);

alert('Years: ' + years + ', ' + 'Months: ' + months);

I would expect the above to return 0 for years and months values, however it returns 3. The years value seems to be returning wrong for seconds values less than 32. And the months value seems to be returning wrong for seconds values less than 4.

Any ideas what is wrong with my code?


That code is doing hte following (I’m doing years right now)

1/60. Then divide that by 60. Then divide that by 24. Then divide that by 365.

The end result for that is 3.170979198376459e-8

That’s being parsed to the nearest integer. 3.

Now, to confirm, let’s change that to 2 seconds.

2/60/60/24/365 = 6.341958396752917e-8

So if we throw that up as a demo, years will be parsed as 6.

Your logic is pretty flawed though. How do you expect 1 second to be any sort of positive year value?

300,000,000 as a value(example) / 60 / 60 / 24 / 365 = 9.5 which gets parsed to 9 years. That’s correct.

This entire thing is just weird logic. Basically, you’re getting 3 because the number is SO small. Seconds to YEARS?

1 Like

Hi, thanks for the reply.

Isn’t 0 the nearest integer to 3.170979198376459e-8? Why does it return 3?

Here is my logic: 1 seconds is 0 years. Is that flawed?

I am not specifically converting 1 seconds to years, it is more like an input box and when I enter anything less than 31536000 seconds, it should display 0 for years. It doesn’t when I enter a value less than 32.

Now I can put some “if” check to fix that but I shouldn’t have needed that.

parseInt will parse INTEGERS, not decimals. As ludicrous as this seems, to me, you could try parseFloat.




Thanks for the input. I need integer results, not floating numbers. parseInt is what I need in my case, it just doesn’t return correct values for very small numbers like 3.170979198376459e-8 etc. It should return 0, but it returns 3.

Because 3.170979198376459e-8 is a number and parseInt expects a string to be passed.

> console.log(typeof 3.1709791983764586e-8);

As mentioned in the docs for parseInt if the passed value is not a string then it is converted to one. In effect you are going the following.

> parseInt("3.1709791983764586e-8");

The reason it outputs 3 is explained in the docs. Emphases is mine.

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.

For example.

> parseInt("31471");
> parseInt("3.1709791983764586e-8");
> parseInt("3.147");
> parseInt("3.x47");
> parseInt("3");
> parseInt("0.3");
> parseInt("x3");
1 Like

Why don’t you use Math.floor to get the integer value?

1 Like

David, thanks for the detailed explanation. So, is my only salution to add a check like the following?

if (seconds < 32) years = 0;

Thanks a lot, it appears that is what I should have used. Works perfectly fine now!

To answer your question: Usually, when you try to fix something that is not supposed to do what you are trying to do, you get blinded to other options.

That’s why I love these forums. I know I will always get an answer from a different perspective than I had thought of myself. Glad it works. :smile:

You are using the wrong function - parseInt expects integer input in a base between 2 and 36 and converts it to base 10 whereas what you are supplying isn’t an integer in any number base.

To convert to an integer use Math.floor()

We beat you to it, @felgall :smile:

It happens to the best of us :slight_smile:

For some unknown reason only the first couple of replies displayed when I first saw this thread. I didn’t see any of the answers that explained how to solve it properly. If I had then I wouldn’t have repeated what had already been said.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.