One Month – The Daily WTF

Date:

Share:

Joseph sends us a tried and true classic: bad date handling code, in JavaScript. We’ve all seen so much bad date handling code that it takes something special to make me do the “confused dog” head tilt.

		var months=new Array(13);
		months[1]='January';
		months[2]='February';
		months[3]='March';
		months[4]='April';
		months[5]='May';
		months[6]='June';
		months[7]='July';
		months[8]='August';
		months[9]='September';
		months[10]='October';
		months[11]='November';
		months[12]='December';
		var time=new Date();
		var lmonth=months[time.getMonth() + 1];
		var date=time.getDate();
		var year=time.getFullYear();
		document.write(lmonth + ' ');
		document.write(date + ', ' + year);

We create a 13 element array to hold our twelve months, because we can’t handle it being zero indexed. This array is going to be our lookup table for month names, so I almost forgive making it one-indexed- January is month 1, normally.

Almost. Because not only is that stupid, the getMonth() function on a date returns the month as a zero-indexed number. January is month 0. So they need to add one to the result of getMonth for their lookup table to work, and it’s just so dumb.

Then of course, be output this all using document.write, so we just know it’s terrible JavaScript, all the way around.

[Advertisement]
ProGet’s got you covered with security and access controls on your NuGet feeds. Learn more.

Source link

Subscribe to our magazine

━ more like this

The Four Quadrants of Conformism

July 2020One of the most revealing ways to classify people is by the degree and aggressiveness of their conformism. Imagine a Cartesian coordinate system whose horizontal...

Dark Matter – PostSecret

For twenty years, PostSecret has broadcast suburban America’s hidden truths—and revealed the limits of limitless disclosure. — Meg Bernhard In the early aughts, Frank Warren ran...

Megan Fox’s Pink Cherub Nails Are a Nod to Her New Baby

While each product featured is independently selected by our editors, we may include paid promotion. If you buy something through our links, we may...

638: Hop, Hop, Hop

Pre-show: Chromebook-block evasions Follow-up: At least one person did miss our WWDC sale. 🙁 Vision Pro Corner: StagePlay Blue Man Group Epic vs. Apple The Verge on Apple’s decision making Apple’s...

Colin from Accounts stars confirm season 3 will begin with a time jump

However, speaking to RadioTimes.com on the red carpet for the 2025 BAFTA Television Awards with P&O Cruises,...