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

‘So unchanged it is almost otherworldly’: the oasis town of Skoura, Morocco | Morocco holidays

The first thing I notice when I walk into the oasis is the temperature drop. Then, I hear the birdsong and the rustling of...

Have a good one, either way I guess. – The Bloggess

Today when we were finishing lunch the waiter was like, “How was your day?” and I said, “Good, thanks! A little exhausting, but...

The Best Coach Bags | PS Fashion

- Additional reporting by Balim TezelKyley Warren (she/her) is an award-winning writer and editor with more than four years of experience in the publishing...

Analog(ue) #194: One of the Best Whims I’ve Ever Had

#194: One of the Best Whims I've Ever Had October 10th, 2021 · 110 minutes This one is a big one! Almost too much to even put in...

How Y Combinator Started

March 2012Y Combinator's 7th birthday was March 11. As usual we were so busy we didn't notice till a few days after. ...