Javascript Rant + Blog changes

Older entry: ArmA III admin/zeus scripts

Posted ; Last Updated

I don't like javascript. I never have. But with every small thing I implement in it, I understand a tiny bit better why I don't like it.

The problem

Until now, dates on my blog were all in the UTC timezone, which made them at least somewhat universal. I've been wanting to change that for a while now, and implement some simple javascript magic that automatically converts them to the users local time zone.

The implementation

I knew for a while that HTML has this thing called "time" tags. They are meant for exactly this kind of purpose: when you have a human readable date in whatever format makes sense to the person reading it, and you still want a machine to be able to understand it. Implementing that was no big deal, just a few minor changes in my backend code and the dates were now properly wrapped in a <time> tag with a datetime attribute containing a date and time in ISO 8601 format. The problem starts with the javascript. I don't do much JS, so my first thought was "Where do I start?". I knew I had this <time> element, so I just opened the console in my browser and started typing document.getElementByTag... and, as it turns out, it's elements (plural, because it returns more than one (duh)), and does not return an array, but a HTML collection. Why it does this is beyond me, but I sure felt the consequences. More on that later though. As I was writing a function to do the actual magic, though only on a single time element, I noticed that there was no information on how to actually turn the date object back into a formatted string. Turns out, that's not possible. You have to just accept javascripts idea of a date and be happy with it. But it gets better. Once I'm done with that rather simple function, I think of an easy way to run it on the time elements, and remember there's this useful thing called map. Though this isn't 100% the intended use case of the map function, it still provides exactly what I need, except, it doesn't. The problem is, it only works on arrays. Why? who knows, though the more important question should be, why document.getElementsByTagName doesn't return a normal array in the first place. In the end I fixed the problem with some terribly ugly code, but the question remains:

Javascript! Why you do this?

Looking on the bright side: the dates are now localized (and above the articles).