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

Heresy

April 2022One of the most surprising things I've witnessed in my lifetime is the rebirth of the concept of heresy.In his excellent biography of Newton,...

Reckless brother demands sister co-sign on new luxury car after driving himself to bankruptcy: ‘I’m scared he’ll ruin my good credit. I have my...

Family and finances… finances AND family! The ultimate recipe for awkward conversations, simmering resentment, and the occasional shouting match. You really need to be a...

Primark launches clothing range designed for people with disabilities | Fashion

It’s a go-to shop for cheap knickers and designer dupes, but now Primark hopes to become the top destination for clothing designed for those...

Westman Atelier Petal Collection 2025

Westman Atelier launched her Petal Collection for spring and it’s beautiful! The Petal Baby Cheeks Blush Stick ($48) has been a staple for me...

Best Clothing Subscription Boxes for 2025

CNET’s expert staff reviews and rates dozens of new products and services each month, building on more than a...