I am working with comparing dates. The problem I am having is that August (08) comes AFTER July (07). However, it seemed to consider Aug (08) and Sep (09) to be earlier than Jan-Jul. When I took a look at the values, they were shown as...

July = "07"
August = "00"
September = "00"

I remember a while back learning that the "default" number system used was the (Oct?) system...0-7 and that there was a function to tell it to recognize the number as coming from a decimal (1-10) system. I just cannot for the life of me remember what that function is.

Any help is greatly appreciated. Thanks.

SnotRockit
:?:

Dani AI

Generated

The behavior described by is the classic "leading-zero = octal" gotcha. As noted, some JS engines historically treat a string or literal that starts with 0 as octal; that makes "08" and "09" parse as 0 (the parser stops at the invalid octal digit). 's short answer — force decimal — is the right direction.

Safer, modern fixes:

  • Convert the month strings with Number() or the unary + operator; both always yield a decimal number and avoid the octal ambiguity.
  • For full date comparisons, use JavaScript Date objects and compare their numeric values (Date.valueOf()/getTime()) instead of manually parsing month strings.
  • If parseInt must be used, ensure you explicitly specify the radix (10) so older engines do not assume octal.

Example workflows (illustrative):

var m = "08";
var n = Number(m);   // 8
var p = +m;          // 8

var a = new Date(2006, 6, 1); // July (monthIndex 6)
var b = new Date(2006, 7, 1); // August (monthIndex 7)
if (a < b) { /* July before August */ }

Troubleshooting tips: log typeof and the raw conversion results to see whether you actually have numbers or strings; if you see "00" after padding, check the conversion step — a 0 value padded to two digits becomes "00". For background and compatibility notes, see the parseInt guidance on MDN: parseInt — MDN and the Number/Date references on MDN for safe conversion patterns.

Recommended Answers

All 3 Replies

You've not supplied enough information. I'm not familiar with any octal-to-decimal operator, sorry.

Perhaps you could post the relevant code?

I remember a while back learning that the "default" number system used was the (Oct?) system...0-7

The default system is decimal, but if you put a '0' in front of your number it is Octal. But you would need to do your assignments without the string operator for this to take effect.

Read this:

Or play with this:

<html>
<body>
<script language="javascript">

document.writeln("08 = " + 08 + "<br/>");
document.writeln("010 = " + 010 + "<br/>");
document.writeln("0x19 = " + 0x19 + "<br/>");
document.writeln("0xFF = " + 0xFF + "<br/>");

var string08 = "08";
document.writeln("string08 = " + string08 + "<br/>");
document.writeln("parseInt(string08, 8) = " + parseInt(string08, 8) + "<br/>");
document.writeln("parseInt(string08, 10) = " + parseInt(string08, 10) + "<br/>");
document.writeln("parseInt(string08, 16) = " + parseInt(string08, 16) + "<br/>");

document.writeln("8 = " + 8 + "<br/>");

</script>
</body>
</html>

It seems if you are silly, then your webbrowser will just assume you don't know what you are doing and use decimals.

If you're using parseInt to obtain 08 or 09's, You just have to set the redix (the numeral system) as 10 to get numbers in decimal system. Like this: parseInt(08,10). Bye!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.