Categories: JavaScript

Subtle, But Important

The amount of work to get this,screen-shot-2016-10-05-at-4-00-21-pm

instead of this,

screen-shot-2016-10-05-at-4-01-58-pm

is the difference between a two week vacation and two weeks of research.

Now, I’m not the world’s best computer scientist. I am not, in point of fact, an accredited computer scientist. I am merely, as the legend goes, a fortunate hacker with delusions of grandeur. That being said, determining the difference in month between one date and another is, as the saying goes, non-obvious.

Originally, our little Slack timebot just assumed a month was thirty days and made calculations based on that simple and incorrect assumption. Javascript doesn’t have much in the way of date manipulation tools, though what it does have turns out to have come in pretty handy.

When it became apparent I both a) couldn’t live with myself having written a bot that assumes every month consists of thirty days and several other incorrect assumptions, and b) I don’t have the API-writing chops to create my own date-manipulation class, I began my search. I started out trying to port Sunshine’s DateDiffInDays calculator from C# to Javascript. Not as insane as it sounds, but I got a little lost in that implementation. There was just too much there for me to port.

Instead, I found jsDate which is written in Javascript. That turned out to be just what the doctor ordered. I had to make some changes to that code’s assumptions. First, it assumed that if date1 was in the same month as date2, and date1 was before date2, that it still constituted the passing of a month. I didn’t want that. I wanted only full months. The remainder would be printed out in days.

I’ll upload the bits of code and the whole bot itself in the days ahead. There’s still some testing to do and cleaning up the code into a package I can upload here.

I feel like I’ve really accomplished something. It’s not anything a second-year CS couldn’t do, but I set out the goal for myself to follow the spec that I wrote up for the Slackbot and I did that.

Now I can look Timebot in the parameters and sit tall.

Article info