r/webdev • u/riccardoforina • Aug 27 '12
Moment.js - Parse, validate, manipulate, and display dates in javascript.
http://momentjs.com3
u/wierdaaron Aug 27 '12
I used Moment.js for a project once where I had to generate a calendar dynamically and populate it with events based on their time stamps. I found it very easy to work with, especially compared to the baseline date functions in JavaScript (which are terrible).
I did find the way Moment handles instances a little weird at first, but I got used to it.
3
Aug 27 '12
Nice. Been using Date.js but always good to have alternatives; and this really seems like a good alternative.
My darkest JavaScript memories are with the native Date Object, so I am really glad other good developers are abstracting this work away from me :)
4
u/aladyjewel Aug 27 '12
Scumbag JavaScript Date object:
.get*Date() returns 1-indexed number
.getAnythingElse() returns 0-indexed number5
Aug 27 '12
^ that, seriously.
If both dates and months started on 1 I could maybe convince myself that the developers made an intelligent design choice.
Dates/months are so burned into everyone's head that representing dates/months internally as they are "in the real life" seems logical to me.
But I can't find one good explanation for choosing this inconsistency between date and month in JavaScript.
And of course, changing it now would probably short circuit the interweb.
2
u/aladyjewel Aug 27 '12
At least it's kinda internally consistent: the constructor takes a 1-indexed number for the day param. I wish it were named "date" to be slightly more obvious, but that would just highlight the awkwardness of the naming/numbering.
0
Aug 27 '12
I remember reading that Brendan Eich based it of Java.
Also, while we have multiple different ways of naming months (January, Jan, and 1) and multiple ways of naming days of the week (Sunday, Sun, 1), the only one way of naming days of the months is by their numerals (1, 1st).
Having the month zero indexed makes it easier to keep an array of month names and use the month to index into the array.
I agree, it's a pretty unfortunate design decision that is constantly causes confusion.
1
u/lumponmygroin Aug 27 '12
I've been using this library and it's great.
One thing I'm unsure about that I believe Date.js does is something like "find last monday". Is that possible? I have a solution already with some standard JS.
1
Aug 27 '12
So good I wish it were part of the standard toolkit (seriously, wtf doesn't JS have a date format function?). I made heavy use of it when building my date picker.
1
u/colindean Aug 28 '12
Great responsive developer. Submitted a translation and it was accepted in a matter of hours.
7
u/200iso Aug 27 '12
Anyone else think 4.8k is kind of big? (Not that it matters in most situations, I'm sure)