Skip to main content

Ralsina.Me — Roberto Alsina's website

Javascript Makes Me Cry: Turning a Date into a String

Work­ing late last night in Al­va I want­ed to do some­thing that sound­ed triv­ial:

When the page load­s, get the cur­rent date and time, and if a cer­tain in­put is emp­ty, put it there like this:

28/05/2013 23:45

So, how hard can that be, right? Well not hard, but...

Getting the current date-time is easy: now = new Date(); So, is there something like strftime in Javascript? Of course not. You can get code from the usual places and have a untested, perhaps broken, limited version of it. And I am not about to add a strftime implementation to use it once. Sure, there are a number of Date methods that convert to strings, but none of them lets you specify the output format. So, let's try to do this The Javascript Way, right?

To get the el­e­ments I want to put in the val­ue, I used ac­ces­sor meth­od­s. So, ob­vi­ous­ly, these should give me what I want for the string, right?

now.get­Day(), now.get­Mon­th(), now.getYear(), now.getH­our() now.get­Min­ute()

Well, they are, at the date men­tioned above, re­spec­tive­ly: 2, 4, 113, er­ror, er­ror

Ok, the errors are easy to fix from the docs. It's actually getHours() and getMinutes(), so now we have 2, 4, 113, 23, 45 and of those five things, the last two are what one would expect, at least. Let's go over the other three and see why they are so weird:

Date.getDay() returned 2 instead of 28

Because getDay() gives you the week day and not the day of the month. Which is absolutely idiotic. So, you have to use getDate() instead. Which means the name is a lie, becasue the logical thing for getDate() to return is the whole date.

Date.getMonth() returned 4 instead of 5

Because getMonth() returns months in the [0,11] range. Which is beyond idiotic and bordering in evil. Come on, Javascript, people have been referring to may as "5" for nearly two thousand years now! What other language does this? Anyone knows one?

Date.getYear() returned 113 instead of 2013

Because it uses offset-from-1900. Which is amazing, and I had never heard of a language doing in a standard type. Because why? So, use getFullYear() instead.

Now, armed with the right 5 num­ber­s, let's for­mat it. Does Javascript have the equiv­a­lent of sprintf or for­mat ? Of course not. In JavaScrip­t, with­out 3rd par­ty mod­ules, you cre­ate strings by ad­di­tion, like a cave­man. Again, I know I could add a for­mat method to the String pro­to­type and make this work, but I am not adding an im­ple­men­ta­tion of for­mat or sprintf just to use it on­ce!

So, this pro­duces that I wan­t:

now.getDate()+'/'+(now.getMonth()+1)+'/'+now.getFullYear()+' '+now.getHours()+':'+now.getMinutes()

Un­less... the day or month are low­er than 10, in which case it's miss­ing the left­-­padding ze­ro. Luck­i­ly, for the pur­pose I was us­ing it, it worked any­way. Be­cause OF COURSE there's no in­clud­ed func­tion to left­-­pad a string. You have to do it by ad­di­tion. Or, of course, add a 3rd par­ty func­tion that's out there, in the in­ter­net, some­where.

Nothing Ever Really Goes Away On The Internet: ra-plugins

I used to man­age a large num­ber of QMail in­stal­la­tion­s. And be­cause Qmail was ... weird­ly li­censed, I wrote a set of plug­ins that ran on top of a patch called Qmail-SP­P. I pret­ty much stopped do­ing that years ago be­cause life took me in oth­er di­rec­tion­s, and for­got all about it.

That col­lec­tion is called ra-­plu­g­ins and I had not touched it since late 2008.

And to­day... I got a patch with two whole plug­ins to add to it so that it makes Qmail han­dle email ad­dress­es more like Gmail does (alias­es us­ing user+­foo and mak­ing us­er.­foo the same as user­foo).

So, I got them, added them, fixed a few sim­ple build­ing is­sues, up­dat­ed the lib­smtp it us­es in­ter­nal­ly for one of the plug­ins to a lat­er ver­sion, and there it stays, per­haps not to be touched un­til 2018.

(Re)Introducing Alva, a Nikola Server

Over a year ago (time flies!) I post­ed some­thing about a project called Al­va. Let me quote my­self:

Al­va is al­most the op­po­site of Niko­la. If Niko­la is about mak­ing stat­ic sites, Al­va is a dy­nam­ic site. How­ev­er, as Hegel sug­gest­s, from the the­sis and the an­tithe­sis comes the syn­the­sis.

So, Al­va is about dy­nam­i­cal­ly cre­at­ing stat­ic sites. If you want to have Niko­la in your serv­er in­stead of in your own com­put­er, and have the con­ve­nience of an on­line tool, that's the niche Al­va tries to fil­l.

So, you would in­stall Al­va, and use it like any oth­er we­b-based blog­ging tool. Yet, be­hind the sce­nes, you would have Niko­la, and all the per­for­mance and se­cu­ri­ty ben­e­fits of stat­ic sites.

And maybe some­day, I (or some­one) will put up a mul­ti­-us­er ver­sion of Al­va, and you will be able to get host­ed blogs, know­ing all the da­ta is yours and you can leave any­time and do your own thing.

The ap­proach I was tak­ing at the time proved to be un­suc­cess­ful, and there were a few oth­er fail­ures along the way. Of course, the prob­lem was in how I was ap­proach­ing the task. So I did the right thing, and learned how to do it "right".

Still not us­able, still not host­ed any­where, but al­ready semi-­func­tion­al: Al­va lives now

There's a lot of work still to be done. But I now know how to do it. To pre­vent the usu­al ar­gu­ments, here is a lit­tle ex­pla­na­tion of mo­ti­va­tion, tool­ing, etc.


I want a way to host blogs very cheap­ly. How cheap­ly? I want at least 1000 rea­son­ably ac­tive users in a $5 VP­S. That would make Al­va a rea­son­able al­ter­na­tive to host­ed mul­ti­-us­er word­press, which means it would be a rea­son­able so­lu­tion (if set­up is easy enough) for smal­l­-­to-medi­um or­ga­ni­za­tions which don't want to set­up ex­pen­sive in­fra­struc­ture yet want to own their da­ta (think school­s, small busi­ness­es, FLOSS pro­ject­s, etc.) I al­so want to pro­vide that ser­vice, for free. Which is why an­oth­er rea­son I want it to be su­per cheap.

How does Al­va help pro­vide this su­per-cheap blog host­ing?

  1. It needs to scale fol­low­ing the num­ber of ed­its not views.

  2. If it gets too busy with ed­it­s, changes take longer to ap­­pear, but the site it­­self does­n't get any slow­er.

  3. Ed­it­ing and serv­ing can be pro­vid­ed by sep­a­rate ser­vices, so I can use some su­per-­­fast stat­ic file serv­er and a su­per-­­con­­fig­urable WS­­GI de­­ploy­­men­t.

  4. In­­di­vid­u­al pages can be heav­i­­ly op­ti­mized so that they down­load fast


One of the guid­ing prin­ci­ples here is that to de­liv­er this sort of thing, in my spare time, the de­vel­op­ment process needs to be stingy with the most lim­it­ed re­source: me. I can't spend a lot of me here. I need to be care­ful and not over-promise.

So, when­ev­er there was a 3rd-­par­ty tool that saves a sig­nif­i­cant amount of time, that's what I am us­ing.


Be­cause it has a much stronger 3rd-­par­ty toolset than Flask or any mi­cro-frame­work. For ex­am­ple, the Flask equiv­a­lent of djan­go-al­lauth broke my will to live. Be­cause the ad­min in­ter­face means I can start adding da­ta to see if it makes sense be­fore I write all the re­quired views.


Be­cause I don't want you to have to cre­ate ac­counts here un­less you want to, this pro­vides (op­tion­al) so­cial lo­gin and reg­is­tra­tion. This was easy to set­up and works alm­sost out­-of-the-box

Bootstrap and Django-bootstrap-toolkit

Niko­la is al­ready heav­i­ly in­vest­ed in boot­strap, so it just made sense to go fur­ther down that road. I un­der­stand boot­strap, and djan­go-­boos­t­rap-­toolk­it is easy enough (although I can't make their datepick­er work)


Be­cause fight­ing is bor­ing.


Be­cause Djan­go's mech­a­nisms to find tem­plates and stat­ic files are many and con­fuse me.

Redis + RQ + django-rq

It's crucial for the whole approach to use job queues in order to detach the rendering from the actual Django app. This combination makes job dispatching ridiculously easy, setup is trivial (install everything, start redis, a few lines of config, ./ rqworker and off you go) and they provide a django admin page where I can see the failed jobs, which is awesome.


Be­cause it's easy enough, and al­lows me some free­dom ex­plor­ing da­ta or­ga­ni­za­tion in my mod­els with­out com­mit­ting to it for­ev­er or recre­at­ing data­bases with test da­ta all the time.


I will prob­a­bly serve the gen­er­at­ed sites via gatling just like my cur­rent sites be­cause it has the sim­plest named do­main con­fig­u­ra­tion pos­si­ble, it's fast and very light in re­source us­age.


A cool, sim­ple ed­i­tor with live pre­views that sup­ports al­most ev­ery markup. Not WYSI­WYG or even WYSI­WYM so pos­si­bly I will have to add an al­ter­na­tive. I start­ed us­ing djan­go-­mark­it­up but it's not a good idea (it us­es a old ver­sion of mark­it­up which re­quires JQuery < 1.9) and am in the process of just us­ing Mark­it­up man­u­al­ly.

So, feel free to give Al­va a try and/or give me a hand, com­ments wel­come.


Es­to es para ayu­darme en una char­la que voy a dar en el Py­Day Rosario por fa­vor re­spon­dan hon­es­ta­mente!


Contents © 2000-2024 Roberto Alsina