logo.gif
Ph: 01092008

Comment On Thank you, Javascript

Originally posted by "Charles Capps"... [expand full text]

Originally posted by "Charles Capps"...

I was having a very, very difficult afternoon. After over a year of pondering, we finally had an excuse to wedge a date widget into our web-based intranet app. Unfortunately, the Javascript library that we chose doesn't exactly have a plethora of widget options unless you're also using the underlying platform the library likes. Yes, I'm talking to you, Prototype. Stop that, it's annoying.

The date widget we picked is hardly feature-packed, but it gets the job done well. Well, rather, it gets the job it was designed for done well. It wasn't designed to be a real date widget. It's just a calendar with decent support for doing whatever you want with it, and it even favors YYYY-MM-DD like we already use throughout our app.

Something it didn't do was automatically change its own date when the user manually edits the date field while the widget is active. It was an annoying problem with an easy and straightforward fix. But then I started testing. Whenever I picked a date in August or September, the widget wouldn't update. Every other month worked fine. Deeply confused, I dug in more. The 8th and 9th of the month also wouldn't update the widgets. Confused, I dove back into the code.

var date_bits = element.value.match(/^(\d{4})\-(\d{1,2})\-(\d{1,2})$/);
var new_date = null;
if(date_bits && date_bits.length == 4 && parseInt(date_bits[2]) > 0 && parseInt(date_bits[3]) > 0)
    new_date = new Date(parseInt(date_bits[1]), parseInt(date_bits[2]) - 1, parseInt(date_bits[3]));

'2008-09-01' was coming out of that regex as [ '2008-09-01', '2008', '09', '01' ], so no problem there.

parseInt('2008') == 2008, good.

parseInt('09') == ...

If you guessed 9, you fail. No, it's zero. See, Javascript supports octal numbers. Any number starting with a zero is octal, even if it can't be an actual octal number. In certain languages, like Perl, trying to use a non-octal number as an octal number results in an error. In other languages, like Javascript, it silently fails.

So, thank you Javascript, for teaching me something I didn't know about you... and making me hate your quirks all the more.

« Prev Page 1 | Page 2 | Page 3 Next »

Re: Thank you, Javascript

2008-04-24 08:06 • by Josh (unregistered)
parseInt('09', 10) will work, second param is the base. The real WTF is Protoype. And the calendar plugin. And JavaScript.

Re: Thank you, Javascript

2008-04-24 08:09 • by Adriano (unregistered)

Re: Thank you, Javascript

2008-04-24 08:09 • by Dave (unregistered)
The real WTF is Javascript. The sooner we junk this crap and start over with nice per-OS browser app which lets you do all the things you'd expect a program to allow you to do in 2008. We could ditch this html bollocks and let people design sites (sorry, apps) which look how you want them to look to the pixel, and use modern security such as that on Vista/Xp etc to allow/deny access to resources.

Re: Thank you, Javascript

2008-04-24 08:11 • by terepashii (unregistered)
It's a known feature, you must use parseInt(String,radix), where radix = 10 to tell it's decimal (2 for binary, 16 for hexa, etc.).
When the value is not precised, javascript tries to guess, and since the prefix for octal numbers is "0", "01" to "09" are transformed into respective octal values 1 to 7 then 0 and 1, hence the strange results for august (08 octal = 0 decimal) and september (09 octal = 1 decimal).
I don't see a WTF here, every javascript coder must have met this strange behavior.

Re: Thank you, Javascript

2008-04-24 08:15 • by dtech
Classic example of "it's not a bug; it's a feature". That's why it has a second parameter.
You could only say the implentation in the browser needs better guessing functionality.

Re: Thank you, Javascript

2008-04-24 08:21 • by Schmitter (unregistered)
I thought it was "its not a bug, it is an undocumented feature." Seriously 2008 and computers still can't handle dates well? That is the WTF.

Re: Thank you, Javascript

2008-04-24 08:22 • by Dave (unregistered)
191317 in reply to 191310
[image]terepashii:
I don't see a WTF here, every javascript coder must have met this strange behavior.


Everyone who met the strange UK alcohol restrictions, only recently relaxed, which stated that you couldn't buy a drink in a pub after 11pm still experienced a WTF moment - especially visitors to the UK who seemed to find it especially amusing.

Re: Thank you, Javascript

2008-04-24 08:25 • by ParkinT
191318 in reply to 191309
[image]Dave:
The real WTF is Javascript. The sooner we junk this crap and start over with nice per-OS browser app which lets you do all the things you'd expect a program to allow you to do in 2008. We could ditch this html bollocks and let people design sites (sorry, apps) which look how you want them to look to the pixel, and use modern security such as that on Vista/Xp etc to allow/deny access to resources.

You, sir, just described Adobe FLEX!

Re: Thank you, Javascript

2008-04-24 08:27 • by ParkinT
191320 in reply to 191315
[image]Schmitter:
I thought it was "its not a bug, it is an undocumented feature." Seriously 2008 and computers still can't handle dates well? That is the WTF.

That's because PEOPLE cannot handle dates well:
2008-09-01

01/09/08
Sept. 01, 2008
September 1st, 2008
... et cetera, et cetera 01-09-2008

Re: Thank you, Javascript

2008-04-24 08:30 • by Anon (unregistered)
You want to see the Real WTF, try doing the following in a JavaScript console.

> parseInt("085");
0
> eval("085");
85

See, parseInt("085") will decide it's 0 because it can't match an octal number.

The JavaScript language ITSELF will merrily say "oops, that's really a decimal" and treat it as such.

Of course, the behavior of parseInt(str) is really wrong anyway, since it HAS an error return: NaN. If the given value cannot be parsed, it SHOULD return NaN!

Re: Thank you, Javascript

2008-04-24 08:51 • by Pawel (unregistered)
Hm, I thought every sane javascript developer has become extinct. I must be wrong or the world is twitching apart.

Re: Thank you, Javascript

2008-04-24 08:51 • by dtech
191325 in reply to 191318
[image]ParkinT:
[image]Dave:
The real WTF is Javascript. The sooner we junk this crap and start over with nice per-OS browser app which lets you do all the things you'd expect a program to allow you to do in 2008. We could ditch this html bollocks and let people design sites (sorry, apps) which look how you want them to look to the pixel, and use modern security such as that on Vista/Xp etc to allow/deny access to resources.

You, sir, just described Adobe FLEX!


Or MS Silverlight. I actually like XAML.

Re: Thank you, Javascript

2008-04-24 08:53 • by rettigcd
Just because you don't know the particulars of the language doesn't make it a WTF.

Some people thing the ?: operator in C/C++/C# is quirky. Does that make it a WTF?

If we classify any exception to the standard as a WTF, then 80% of the English language is guilty.

The real WTF is that the writer thinks he knows Javascript.

Re: Thank you, Javascript

2008-04-24 08:55 • by Tukaro
191327 in reply to 191307
[image]Josh:
parseInt('09', 10) will work, second param is the base. The real WTF is Protoype. And the calendar plugin. And JavaScript.
I had the same issue and found the same solution.

I believe Javascript will return NaN. But really, I wish browsers would just fail things like Javascript and HTML when the developers can't be arsed to actually implement them right. This would have stopped IE in its tracks long ago, but no, we had to be "kind".

It would also make it easier to find errors instead of having to parse through everything yourself.

Re: Thank you, Javascript

2008-04-24 09:03 • by Lawrence (unregistered)
This has to be one of my biggest pet peeves with current programming languages in general. Why the HELL is there any constant which is inferred to be octal? I have never used an octal constant in my life. Decimal, sure! Hex, all the time. But octal - NEVER, EVER, EVER, NOT EVEN ONCE!

PS: I note this is different from Java Integer.parseInt() which treats a String as decimal unless a specific radix is supplied - though Java, too, does treat literals as octal if they have a lead 0.

Re: Thank you, Javascript

2008-04-24 09:05 • by fraserofthenight (unregistered)
Some people thing the ?: operator in C/C++/C# is quirky. Does that make it a WTF?


What's the quirk? It works just as I would expect it to in Java/D; is it different in C/C++/C#?

Octal must die

2008-04-24 09:09 • by Anon Fred (unregistered)
191332 in reply to 191328
[image]Lawrence:
Why the HELL is there any constant which is inferred to be octal?


Because Perl did the same thing!

The only thing octal has ever been good for is file permissions. Octal should be shot in the head, driven over a cliff, shot in the head again, and then incinerated.

Re: Thank you, Javascript

2008-04-24 09:09 • by Zerojack (unregistered)
var date_bits = element.value.match(/^(\d{4})\-0*(\d{1,2})\-0*(\d{1,2})$/);

Re: Thank you, Javascript

2008-04-24 09:16 • by Anonymous Coward (unregistered)
191339 in reply to 191320
Agreed.

And don't even get me started on 09/01/2008.

Re: Thank you, Javascript

2008-04-24 09:18 • by steve (unregistered)
I had a similar WTF with jsript date method calls. Apparently jscript date objects have the "feature" of resetting the day of month to 1 if the day is set prior to the month:

var month = 3
var date = 31
var year = 2008

var fromDate = new Date;
fromDate.setDate(date);
fromDate.setMonth(month - 1); // January = 0
fromDate.setFullYear(year);

WScript.Echo(fromDate);

Re: Octal must die

2008-04-24 09:23 • by Aaron
191341 in reply to 191332
[image]Anon Fred:
The only thing octal has ever been good for is file permissions.

And networking.

And 7-segment displays, but if you're using JavaScript to control one of those then you might want to rethink your design...

Re: Thank you, Javascript

2008-04-24 09:24 • by Russ (unregistered)
191342 in reply to 191318
[image]ParkinT:
[image]Dave:
The real WTF is Javascript. The sooner we junk this crap and start over with nice per-OS browser app which lets you do all the things you'd expect a program to allow you to do in 2008. We could ditch this html bollocks and let people design sites (sorry, apps) which look how you want them to look to the pixel, and use modern security such as that on Vista/Xp etc to allow/deny access to resources.

You, sir, just described Adobe FLEX!


I'm pretty sure you're confusing FLEX with AIR.

Re: Thank you, Javascript

2008-04-24 09:25 • by Therac-25 (unregistered)
Don't blame Javascript, blame atoi.

I've seen this same behaviour elsewhere, it's because it's dropping down to C's standard library.

Re: Thank you, Javascript

2008-04-24 09:28 • by Random832
191344 in reply to 191321
[image]Anon:
Of course, the behavior of parseInt(str) is really wrong anyway, since it HAS an error return: NaN. If the given value cannot be parsed, it SHOULD return NaN!


But it can parse it. Well, part of it.

parseInt("9abc") == 9

parseInt("05585") == 45 == 055

@steve

2008-04-24 09:34 • by Chris Hunt (unregistered)
191345 in reply to 191340
"Apparently jscript date objects have the "feature" of resetting the day of month to 1"

I've never used jscript, so i'm just guessing here, but could it be that the date object defaults to the current month and year if you haven't yet put them in when setting the date part? So if you start off by setting the date to 31, and the current month has 30 days, it silently fails and sets the date to 1 instead. That would still suck, but also make some sense.

In the interests of science, this might be interesting to run:

var month = 3 

var date = 31
var year = 2008

var fromDate = new Date;
WScript.Echo(fromDate);
fromDate.setDate(date);
WScript.Echo(fromDate);
fromDate.setMonth(month - 1); // January = 0
WScript.Echo(fromDate);
fromDate.setFullYear(year);

WScript.Echo(fromDate);

Re: Thank you, Javascript

2008-04-24 09:36 • by chishm
There's a similar feature in Windows Vista (maybe XP too). Edit an MP3's tags using the summary page of the file's properties. Now enter track numbers 01, 02, etc. It will reject your attempt to save track 08 and 09, but 10 will be fine.

Re: Thank you, Javascript

2008-04-24 09:36 • by spacix
[image]Tomorrow on The Daily WTF:

Wow, I can't believe this problem! I have some error in my JavaScript event and it keeps pointing me to the event caller! I can't track this bug, why didn't they include something to help track this!

Then we can post about the joys of the "debugger;" command...

If this a WTF, then all of the people who try to float two <div>'s in HTML side by side are building a WTF.

Anyone who knows anything about HTML knows that if you want two things side-by-side you use the <span> tag, which doesn't include a break before and after it!

Re: Thank you, Javascript

2008-04-24 09:39 • by mister (unregistered)
191348 in reply to 191340
@steve: the problem here is that the default value of a javascript date object is the current date. First, you are setting the day of the month to 31. But wait! We are in April, which only has 30 days! So, it tries to set April 31th, and we end up with May 1st (the day after April 30th). Then you set the month to March, we end up with March 1st.

Instead, set the full date at once:
var fromDate = new Date(year, month-1, date);

Re: Octal must die

2008-04-24 09:42 • by doynax (unregistered)
191349 in reply to 191332
[image]Anon Fred:
The only thing octal has ever been good for is file permissions. Octal should be shot in the head, driven over a cliff, shot in the head again, and then incinerated.
Well.. Octal makes perfect sense if you're working with 18 or 36-bit hardware, and considering the roots of C it's hardly surprising that we're still stuck with it. And I suppose dropping it from a new language would lead to all kinds of fun bugs when back-porting code.

My personal pet peeve is that most languages still don't support binary constants.

Re: Thank you, Javascript

2008-04-24 09:43 • by Russ (unregistered)
Anyone who says they never use octal and octal has no use probably just don't recognize that they are using it or that some utilities assume you are passing octal on the command line.

0755 anyone?

And BTW, if JavaScript really has a NaN representation for int, I think *that* is a WTF.

Re: @steve

2008-04-24 09:44 • by spacix
191352 in reply to 191345
[image]Chris Hunt:
So if you start off by setting the date to 31, and the current month has 30 days, it silently fails and sets the date to 1 instead.
What it will do is set the date to the the first of the next month.

Example:
Will set the date to January 365, 2008 it'll make the date January 30, 2009.
var d = new Date();
d.setFullYear(2008,1,365);
document.write(d);

Re: Thank you, Javascript

2008-04-24 09:47 • by Manuel (unregistered)
191354 in reply to 191333
[image]Zerojack:
var date_bits = element.value.match(/^(\d{4})\-0*(\d{1,2})\-0*(\d{1,2})$/);

Nice. Would only replace the * with an ?. Although this regex is not complete, because it also accepts "invalid" formats like 2008-012-1.

Re: Thank you, Javascript

2008-04-24 09:48 • by Teh Irish Gril Riot (unregistered)
191355 in reply to 191328
[image]Lawrence:
This has to be one of my biggest pet peeves with current programming languages in general. Why the HELL is there any constant which is inferred to be octal? I have never used an octal constant in my life. Decimal, sure! Hex, all the time. But octal - NEVER, EVER, EVER, NOT EVEN ONCE!


Beware of Allen Bradley / Rockwell PLC addressing.

Re: Thank you, Javascript

2008-04-24 09:52 • by Mike (unregistered)
191356 in reply to 191350
[image]Russ:
Anyone who says they never use octal and octal has no use probably just don't recognize that they are using it or that some utilities assume you are passing octal on the command line.

0755 anyone?


Some of us don't mind typing "a=rx,u+w". It's clearer.

Re: Thank you, Javascript

2008-04-24 09:54 • by Mike5 (unregistered)
191357 in reply to 191350
And BTW, if JavaScript really has a NaN representation for int, I think *that* is a WTF.


Hardly, because JavaScript has no 'int'. Only 'number' which can be of course be a NaN.

Mike5

Re: Thank you, Javascript

2008-04-24 09:54 • by James (unregistered)
I also vote for "no WTF here". System working as designed, which is exactly as correct as the way the OP wanted it to work. If you're going to support octal at all, you have to do it right. And if you're going to provide a way to have the system guess which kind of number you're parsing, you need to do so consistently. So they made up some rules, documented them (right?), and implemented it that way.

The reason it doesn't return NaN is because parseInt is trying to be permissive. Try this:

alert(parseInt("foo"));

alert(parseInt("10 foo"));


The first one returns NaN, while the second returns 10. The *documented behavior* (there we go with that again!) is to try really hard to find a number, then quit if we hit something that's not part of the number. So what the parser encountered is 0 (I'm octal!), 9 (I'm not a number in the current set, so stop parsing), and it returned 0.

That's why they say "I'll treat it as octal if it starts with a 0 and stop parsing once I hit an 8 or 9" instead of "I'll treat it as octal if it starts with 0 and there are no 8s or 9s (otherwise, decimal)".

If there is a WTF, it's using Javascript functions without reading the documentation about what they do.

Re: Thank you, Javascript

2008-04-24 09:57 • by mister (unregistered)
191360 in reply to 191356
0755 anyone?
Some of us don't mind typing "a=rx,u+w".
But... but... it's a 100% increase of keystrokes!

Re: Thank you, Javascript

2008-04-24 09:59 • by Bram Geron (unregistered)
191362 in reply to 191360
Nope, 125%. The plus sign takes two keystrokes on QWERTY ;)

You don't use QWERTY? Respect.

Re: Thank you, Javascript

2008-04-24 10:01 • by Matt (unregistered)
191363 in reply to 191320
[image]ParkinT:
[image]Schmitter:
I thought it was "its not a bug, it is an undocumented feature." Seriously 2008 and computers still can't handle dates well? That is the WTF.

That's because PEOPLE cannot handle dates well:
2008-09-01

01/09/08
Sept. 01, 2008
September 1st, 2008
... et cetera, et cetera 01-09-2008

I keep.... hearing ... this date! IN MY HEAD!

It's some kind of prophecy! RUN FOR YOUR LIVES! :(

Re: Thank you, Javascript

2008-04-24 10:04 • by Anonymous (unregistered)
alert("077" - 0 == 7); (true)
alert("09"-0 == 9); (true)

Well at least on Firefox.

Re: Thank you, Javascript

2008-04-24 10:06 • by fennec
Have you ever considered using 4-digit year strings? I mean, what is this, Y2K8 or something? :P

Re: Thank you, Javascript

2008-04-24 10:12 • by Theo (unregistered)
191371 in reply to 191362
...or maybe he uses the numeric pad ;)

Re: Thank you, Javascript

2008-04-24 10:13 • by Theo (unregistered)
191372 in reply to 191362
What about the numeric pad? :)

Re: Thank you, Javascript

2008-04-24 10:23 • by KenW
191379 in reply to 191362
[image]Bram Geron:
Nope, 125%. The plus sign takes two keystrokes on QWERTY ;)

You don't use QWERTY? Respect.


No, 100%. The plus sign on the numeric keypad is a single keystroke.

You don't use the numeric keypad? Respect.

Re: Thank you, Javascript

2008-04-24 10:24 • by Zecc
191380 in reply to 191362
[image]Bram Geron:
Nope, 125%. The plus sign takes two keystrokes on QWERTY ;)

You don't use QWERTY? Respect.
Hey, my keyboard is QWERTY and I have a '+' key right here over the 'ç' and 'º' !

(On the other hand I need shift+0 to type '=')

Re: Thank you, Javascript

2008-04-24 10:30 • by Zock (unregistered)
191381 in reply to 191362
[image]Bram Geron:
Nope, 125%. The plus sign takes two keystrokes on QWERTY ;)

You don't use QWERTY? Respect.


Spot the false assumption. ;)

Re: Thank you, Javascript

2008-04-24 10:31 • by Dave Ross (unregistered)
This is a Javascript rite of passage. It bites everyone once, and then you never forget.

Re: Javascript Dates

2008-04-24 10:31 • by JimM
191383 in reply to 191352
[image]spacix:
Example:
Will set the date to January 365, 2008 it'll make the date January 30, 2009.
var d = new Date();
d.setFullYear(2008,1,365);
document.write(d);

And that, my friends, is the real Javascript Date WTF. It will try its very best to make some kind of date out of the informaiton you give it, even if that date is totally ridiculous. So instead of saying "That month doesn't have 365 days", it says - well, I'm not sure what, because for the me 365th day of January 2008 would be the 1st of January 2009, not the 30th. All very odd. Is it too much to ask for a function to throw some kind of error if the input doesn't make sense?

Re: Thank you, Javascript

2008-04-24 10:40 • by Chris M. (unregistered)
191387 in reply to 191320
[image]ParkinT:
[image]Schmitter:
I thought it was "its not a bug, it is an undocumented feature." Seriously 2008 and computers still can't handle dates well? That is the WTF.

That's because PEOPLE cannot handle dates well:
2008-09-01

01/09/08
Sept. 01, 2008
September 1st, 2008
... et cetera, et cetera 01-09-2008


Quickly, now: is 01-09-2008 the first of September--or is it the ninth of January (which is how we'd read it where I come from)?

Re: Thank you, Javascript

2008-04-24 10:44 • by Zecc
191388 in reply to 191387
[image]Chris M.:
Quickly, now: is 01-09-2008 the first of September--or is it the ninth of January (which is how we'd read it where I come from)?
Thank you for proving his point.
« Prev Page 1 | Page 2 | Page 3 Next »

Add Comment


You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser