
Fixing JavaScript's Date (2017) - pcr910303
https://maggiepint.com/2017/04/09/fixing-javascript-date-getting-started/
======
jessaustin
This one isn't mentioned:

    
    
      > d
      Thu Feb 27 2020 17:05:28 GMT-0600 (Central Standard Time)
      > d.getMonth()
      1
    

I'm as fond of zero-indexing as anyone, but this is ridiculous.

~~~
cesarb
Even worse, it's not even internally consistent: while the month of the year
starts at 0, the day of the month starts at 1. This is one of the worst API
design mistakes of Java, and Javascript blindly copied it. At least the newer
Java APIs (the java.time ones) fixed that: the month starts at 1 with the
newer APIs (when not using the enum). But I don't have much hope of Javascript
copying the new and better API.

~~~
baddox
The reason there's an "exception" for days is that the numbers are the widely-
used _names_ for the day of the month. Months arguably don't have widely-used
numeric names, although it's pretty reasonably to argue that because numeric
month-and-day formatting is so ubiquitous it would have been okay to 1-index
the months.

It occurs to me that "9/11" is the one date I can think of where the month is
commonly verbally referred to by its number.

~~~
samatman
That's... a terrible argument.

1970-01-01T00:00:00 is the start of the Unix Epoch, it is definitely not
1970-00-01T00:00:00.

This is consistent across all the warty and various ways to represent the
Gregorian calendar, the fifth month is _always_ May, no exceptions.

~~~
strbean
I think parent means that we often say:

> I get my check on the first of the month

But never really say

> I'm going on vacation for the sixth (month) of the year

Seems like they are just trying to explain the bad decision, not justify it.

~~~
baddox
Yes, I'm not trying to justify it. And it's not just that we say "the fifth
day of May," because it's also true that we can say "the third month." My
point is that the fifth day of May is actually named "5" and there's no other
name for dates (other than specially=named days, of course).

------
paulddraper
JS Dates are terrible, but not just the native stuff...there isn't a single
good library either. [1]

A good library instants (1582852000000 ms since epoch), durations (5000 ms),
periods (2 months), offsets (-0700), timezones (America/Denver), date
(4/3/2019), and time (4:45 PM).

Instead I am stuck with good treatment of like two or three of these in any JS
library I pick up -- usually instant and duration -- and I have to hammer
things to fit.

[1]
[https://news.ycombinator.com/item?id=21669829](https://news.ycombinator.com/item?id=21669829)

~~~
jasonkillian
I've found js-joda is a good JS datetime library: [https://github.com/js-
joda/js-joda](https://github.com/js-joda/js-joda)

~~~
paulddraper
Interesting, thanks!

I've used Joda and java.time a lot, and I have a very high opinion of both.

------
jasonkillian
In case it wasn't immediately obvious, there's a Stage 2 tc39 proposal that
was spawned from this blog post called Temporal:

[https://github.com/tc39/proposal-temporal](https://github.com/tc39/proposal-
temporal)

At a glance, it does follow the same ideas as the great Joda Time library (now
a part of the Java standard library), and there's currently a port of this
library for JS which is quite nice:

[https://github.com/js-joda/js-joda](https://github.com/js-joda/js-joda)

------
leftyted
Another oddity of JS's Date is that "Date()" returns a string whereas "new
Date()" returns an object.

This makes it hard to do "class MyDate extends Date" when targeting ES5 from
TS. In fact, if anyone knows an easy solution for that I'd love to hear it.

~~~
why-oh-why
Your request is too broad, I suggest asking a question on StackOverflow
explaining what the issue is exactly.

~~~
leftyted
I didn't make a request. My post consists of two observations and an aside.

Thanks for your wonderful feedback though.

~~~
jessaustin
That is the wrong way to fish for that "easy solution" on HN. The proper way
is to confidently state that there is no easy solution, which may inspire the
HN hive mind to contradict you. That probably won't work ITT however since I
spoiled it by pointing this out.

------
z3t4
Dealing with dates is such a clusterfuck. Avoid it at all costs. Dates are a
mix of political whims, astronomy theories, and historic artifacts, and works
differently depending on what country you are in. With leap years and leap
seconds, holidays, and summer/winter time adjustments.

------
ZeroClickOk
The fix for any date/time issues in any language is replicate NodaTime
([https://nodatime.org/](https://nodatime.org/))

~~~
tybit
NodaTime is great, as is the project it's based on JodaTime, which has since
been deprecated in favor of the standard library java.time.
[https://www.joda.org/joda-time/](https://www.joda.org/joda-time/)

Hopefully other languages and platforms, including .NET, move to including a
sane DateTime library in the standard library too.

------
paulddraper
> Now we can see that lots of issues with date are fixable. However, we are
> left with two things on our list that aren’t: mutability and parsing. The
> reasons that these things can’t be fixed relates to two concepts that TC39
> has to deal with a lot – web compatibility and web reality.

Same with Java.

It was still able to introduce a new date API.

------
zadokshi
We all know it’s broken. More importantly, who’s job is it to fix it?

