- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
The month=1 was fristly implemented in MSDOS, where it returned a 1 .. 12 for month on GetDate
Admin
More things: using
var
,document.write
is called twice when once would suffice, explicit array declaration.Call me Bran, because I can see the genesis of this code: First, they wrote it straight-forwardly, and realized that they got almost the correct result. So they almost did the right thing and began to fiddle with it, leading to the additional lines handling the month. I can even see why it is
lmonth
and notmonth
, because that definition (var month = date.getMonth()
) was deleted when it became unused.Admin
Using the name
time
for a variable that holds a date is a nice touch. Yes, it's actually a date+time object, but our use of it is date-only. I like how at the end they use twodocument.write()
calls each containing at least one concatenation in their arguments. IMO that should have been 5 calls with no concatenations or 1 call with 4 concatenations.OTOH I am impressed that they avoided a typo in the 60-some characters it takes to type out all 12 month names in English. And their array indexes are in order and none are incorrect.
So overall not wrong, just boneheadedly stupid. Brain off, fingers flying. The reeechest kind of code.
Addendum 2024-12-17 07:00: I type too slowly. some guy wasn't there when I started.
Admin
Why do I have the feeling that this dev grew up with PASCAL? lol
Admin
TRWTF is JS for not having a proper datetime library in the language, BUT they could have just created a frozen object (either 0 or 1 indexed) and used it as a "look up table" instead of creating an array every time they need to ... "look something up" ...
Admin
Yes, but they're working on it.
Admin
It all started on the 13th hour of the 13th day of the 13th month. "Lousy Smarch weather!"
Admin
As we can see here, the word "almost" is one of the many real enemies of a programmer: "this code almost works, it will output the right amount of money almost every time", or "the pump will dispense almost the correct amount of insulin", "the car will almost stop at the right point", "the elevator will almost reach the requested level", or even "the plane's fuel display will almost show the correct value". So the solution is to fiddle with the code until the compiler stops complaining and only issues stupid warnings (which can be ignored), or in interpreted languages such as JS, it's good enough when it's almost correct in most cases, sometimes.
Admin
I wonder if the programmer started with code that formatted the date with a numeric month, so it needed to add 1 to
date.getMonth()
. When they changed it to print the month name, they offset the array rather than removing the+1
.Admin
Looking at what this is outputting, it looks like
date=time.getDate()
returns the day of the month. That's not confusing at all...
Admin
This is an outdated criticism. One of the more popular date libraries for javascript - moment.js - has been pushing people to use native language features rather than a library for a long time now. Here is their 2020 article literally telling developer to stop adding the library out of simple habit of "dates suck in js, where's my library". https://dockyard.com/blog/2020/02/14/you-probably-don-t-need-moment-js-anymore
Admin
Date.GetMonth SHOULD return a 1 for January. Date.GetDay returns 1 for the first day of the month so why should only the month be zero indexed?
Admin
That's almost how you get the Gimli Glider (and Tuninter Flight 1153).
Admin
Arrays are normally 0-based, but we refer to months 1-based. Most languages do months in dates 1-based, but JS does them 0-based. So it looks like the developer expected it to work the way most languages do, but it didn't, so he simply added 1 rather than setting the array up starting from 0. If JS offered better date formatting, this would be just a few lines.
Admin
Just because lazy lackadaisical laypeople lay their dates out lazily does not excuse us proper developers doing the same. Instead, it is our scared duty to always, at every step and in all things strive to correct their mischievous misguided misbehavior by adjusting all arrays. without regard to their contents such that they are properly enumerated with a zero at their start.
Admin
The lazy lackadaisical people are the computer language designers who are too inept to allow users to specify the lower bound of their arrays if that's the natural way to index what the arrays represent.
Admin
Beasts we are not, bound by natures rules, but men. Men of logic and reason cold. To ways of nature we do not bend, but shape our own steel of will and icy intellect. We make the choices and know them true. For by us they are chosen, thus, true they must be for true are we. And if others should disagree than it is they who right are not. For they are lesser men than we. Why else indeed would they disagree.
Admin
Date is time though. Just the larger calendar portion of it. Month/Day/Year without clock is still time. Much like specifying hours and minutes of the day but omitting the seconds portion. You've never heard physicists talk about the space-date continuum after all, have you?