-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Off-by-one error in blog dates #4881
Comments
In my case it gets more frustrating, since |
I'm also seeing the same behavior and dove into the bug a bit more, @ysulyma's fix is close, but I think it needs to be a little further down on https://github.com/facebook/docusaurus/blob/master/packages/docusaurus-plugin-content-blog/src/blogUtils.ts#L189. @ysulyma , I found the same thing you did, but was using const formattedDate = new Intl.DateTimeFormat(i18n.currentLocale, {
day: 'numeric',
month: 'long',
year: 'numeric',
timeZone: 'UTC',
}).format(date); I'll review the Docusaurus contributing guidelines and if this is still open in a few days I'll see about opening a PR. In the meantime, I hope this helps. |
@josh-kaplan I think you are on the right track. Are you going to open a PR? Docusaurus is pretty welcome to contributions. |
The behavior of Date with time zones is just entirely cryptic to me... I was able to locate my bug of URL offset to https://github.com/facebook/docusaurus/blob/master/packages/docusaurus-plugin-content-blog/src/blogUtils.ts#L53. Firstly, my file name is like let d = new Date('2021-4-20') // "Tue Apr 20 2021 00:00:00 GMT+0800 (China Standard Time)"
d.toISOString() // "2021-04-19T16:00:00.000Z" I think the easiest fix is to always set the time zone to UTC in creation of the date object rather than in transforming it to strings. |
That's interesting .... so it looks like new Date('2021-06-1') // 2021-06-01T04:00:00.000Z
new Date('2021-6-01') // 2021-06-01T04:00:00.000Z
new Date('2021-06-01') //2021-06-01T00:00:00.000Z I think you're right, @Josh-Cena, that UTC should be explicitly specified both when the date is created and when it is converted into a usable string so that Docusaurus respects that actual date string the author of the file intended. The only other concern still is whether or not this is still an issue for blog URL strings (I haven't tried it yet) Interesting problem. I am curious why the timestamp changes for the different date formats...the MDN documentation isn't super clear about that. |
Yes, I was totally bewildered at first... In fact, if you look at the "Timestamp strings" section in the MDN doc, you see the note that
Where ISO 8601 demands YYYY-MM-DD format. This is the only case where the string is treated as UTC rather than local. In my PR I have enforced UTC whenever we are dealing with dates (the |
I agree with your fix. I'm still baffled by the implicit 08:00 UTC timestamp (16:00 for you, 04:00 for me). But adding the "Z" to the date creation does seem to fix the issue. |
Hmmm. Can you elaborate on the 08:00 thing? As I understand it, when the date object is created in the local timezone, the default timestamp is always 00:00, just that you later convert it to UTC.
|
Yeah, I'm not sure it impacts your fix though so if this becomes too unrelated to the bug, perhaps we should move the conversation elsewhere (it might still be relevant though) ... When I do the following: new Date('2021-06-01') // 2021-06-01T00:00:00.000Z
new Date('2021-06-1') // 2021-06-01T04:00:00.000Z Node defaults to different times for the two date formats (note they are both in UTC). In your case of UTC+0800, It looked like the time was defaulting to From the ECMA Spec
So, if you use the date format I guess the real takeaway here is that your blog filenames should always use the |
Hey, I agree that we should use both UTC for parsing and formatting, as we don't care about the time. We could also use a lib that supports the concept of "local date", which is a given day without a timestamp/timezone. Probably overkill just to fix this bug though. To test this it's a bit annoying/complicated as we'd have to force a certain tz on our CI, and we would have tests that only fail at certain hours of the day. Not sure it's worth investing much in that for now |
🐛 Bug Report
Dates in the blog section are displayed incorrectly. For example, if I create a file
2021-05-10-hello.md
, the url will correctly be/blog/2021/05/10/hello
, but the blog post displays "May 9, 2021".Have you read the Contributing Guidelines on issues?
Yes
To Reproduce
I was unable to reproduce on https://new.docusaurus.io. However, I was able to trace the bug to https://github.com/facebook/docusaurus/blob/master/packages/docusaurus-plugin-content-blog/src/blogUtils.ts#L175.
On my machine (both in Node and in the browser)
new Date()
behaves as follows:I was able to fix the bug on my machine by changing the above line to
I don't want to make a PR myself because I'm not familiar enough with the Docusaurus code (or the Javascript Date functions) to know if this would fix it for all users or introduce bugs elsewhere.
Expected behavior
Dates would be displayed correctly.
Actual Behavior
Dates are not displayed correctly.
Your Environment
Reproducible Demo
See above.
The text was updated successfully, but these errors were encountered: