Dates prior to Jan 2, 1884 show as a day prior in

2019-09-06 01:37发布

问题:

I'm collecting dates from a simple HTML date input field with the following code:

new Date(Number(dateString.substring(0,4)), Number(dateString.substring(5,7))-1, Number(dateString.substring(8,10)))

...and I output these dates to the user with this code:

startDate?((startDate | date | uppercase)

This works fine for ANY DATE AFTER January 1, 1884. So, January 2, 1884 works fine (and any date to today from what I can tell), but if I enter January 1, 1884 it is displayed back as December 31, 1883. Any date prior to January 1, 1884 has the same issue.

I get that it will be converting this back into my local timezone when displayed, but that problem should manifest itself for all dates (I've already encountered that). What happened on January 1, 1884? Why is this date special?

I also notice that if I go WAY back, to January 1, 0100 - a day gets added. So that day becomes January 2, 0100.

I am not sure exactly where the crossover happens there but certainly something funny is going on. If I just output the value of the date to the console I correctly get:

1884-01-01T08:00:00.000Z

...and for the 2nd of that month I get, predictably:

1884-01-02T08:00:00.000Z

So it seems like it is the Angular2 date pipe that is changing it. Is this a bug or am I doing something incorrectly (which is believable)?