Another JavaScript API 💎: months are zero-based, but days and years are not. 😒

  • 5
    For dates and time zones in javascript please use www.momentjs.com
  • 4
    @heyheni Yeah, I wanted to avoid more dependencies, but after I found out the JS date API has no way of validating ISO date strings (it accepts many formats which may lead to e.g. swapping day and month) I gave up and used the mentioned moment library (very powerful and way better API).
  • 3
    Zero-based months? What the fuck
  • 1
    @filthyranter ive always assumed it was so you could use the months in a select box, and using the value to map to a month name array alongside it
  • 3
    Because they expect you to put month index in an array, like

    var months = ['jan', 'feb', 'mar', ....]
    var month = months[new Date().getMonth()];

    This is not needed, instead, for days and years
  • 2
    Use date-fns is better that moment for sure. Problem with moment is that is mutable and if you are performing operations on dates you always have to clone objects, really easy to have bugs there. Also date fns is way smaller and the API is mostly the same
  • 1
    In recent years there were many new browser apis released, is there one for dates?

    Or is there one in web assembly?

    (i have no idea, i can't code)
  • 1
    @heyheni for browsers there is the old JavaScript date.
    Date-fns is a library and by API I meant how to do something with dates. F.e. format(hhmmss)

    Maybe I didn't understand your question
  • 3
    I don't think it was such a great idea having different bases in parameters of one function. For example I wanted to test some operation on dates:

    > const d = new Date(2018, 1, 1);

    So I was (I would say naturally) expecting to get 1st January 2018. Was greeted with crashing test and a very confusing expected value:

    > d.toISOString();


    Where the hell February came from?

    JS has a few nasty traps like map:

    > ['1', '10', '100'].map(Number.parseInt)

    [ 1, NaN, 4 ]

    But I think overall it is an alright language. I view PHP as a much much bigger mess.
  • 0
    @filthyranter that 3 words, wtf explains a lot about using JS frameworks (or even just JS itself) 🤣
  • 0
    @irene map passes three arguments (item, index and whole array) and parseInt accepts optional second argument - radix I think. Some attempts at using FP in vanilla JS give very unexpected results. But with good libraries shielding programmer from this occasional traps, I think JS can be quite successfuly used in reasonable FP.
  • 0
    @irene I think zero radix gets interpreted by parseInt same way as undefined (= not filled optional argument) so it goes with default radix 10. Magic :)
  • 1
    @irene I am aware that 0 is the first value, but devs should try to be consistent. Either all values are zero-based or none for one data structure. Everything else is just gonna be confusing to try out.
  • 0
    @irene So that means...
    Both are shitty high-level languages?
Add Comment