feed icon indicating copy to clipboard operation
feed copied to clipboard

Date formatting to ISO string invalid (adding timezone computation)

Open Arteneko opened this issue 5 years ago • 6 comments

Describe the bug When you create a date from an iso8601 string, and when you try to "render" it, e.g. with date.toISOString() (source: lib/atom.1.js:67), it computes it and works with the time-zone.

I'm in a case in which I want to have absolutely 0 change from the original timestamp, but the JS conversion removes 2 hours from the actual timestamp on render.

Wouldn't it be possible for "updated" to take an iso-formatted string, either validating it or trusting the developer to not fuck up the format ?

To Reproduce Steps to reproduce the behavior:

Tested in UTC+2 time zone

new Date("2018/09/09")
// Returns Date 2018-09-08T22:00:00.000Z

Expected behavior

I'd expect it to return Date 2018-09-09T00:00:00.000Z

More informations

The obtained error is:

TypeError: Cannot read property 'toISOString' of undefined
index.js:36
    at /home/nyx/git/Blog/node_modules/feed/lib/atom1.js:67:34

Because, since I pass a correctly-formatted ISO string and not a Date instance, the validator doesn't take the value, so it evaluates to undefined.

Arteneko avatar Sep 21 '18 10:09 Arteneko

@IvanDelsinne If you don't want to change the timezone of your environment, you can use Date.UTC with vanilla JavaScript:

new Date(Date.UTC(2018, 8, 9)).toISOString()
// "2018-09-09T00:00:00.000Z"

Does that solve your issue?

jpmonette avatar Sep 30 '18 16:09 jpmonette

I'm gonna try to do that, I haven't managed to find this solution anywhere. Thanks for the tip.

Still, allowing to provide a custom date value wouldn't be a good idea ?

Arteneko avatar Sep 30 '18 16:09 Arteneko

@IvanDelsinne Maybe I could allow string-based date, but existing behaviour is to avoid someone messing up the date format 🤔. How would you generate the ISO string otherwise?

jpmonette avatar Sep 30 '18 18:09 jpmonette

I tried that, I'm still getting the timezone conversion, with the following code snippet:

let date = i.data.date.substring(0, 10).split('-').map(i => Number.parseInt(i));
date = new Date(Date.UTC(date[0], date[1], date[2]));

Here, i.data.date is "2018-09-14T00:00:00Z" and I get "2018-09-14T02:00:00Z".

Edit:

It's weird, if I print the data as debug, I get the timezone converted, but if I print theISOString, it doesn't convert.

Arteneko avatar Oct 03 '18 09:10 Arteneko

I'm having this same issue, but only with the atom1() method. It seems to work fine with rss2().

karllhughes avatar Jul 09 '19 22:07 karllhughes

(Perhaps) similar issue with json1() — I get an ISO string but trying to use the date triggers the item.date.toISOString is not a function error. What's getting returned is, for example, 2022-04-06T14:55:00.000Z.

Disregard. Figured it out. My apologies. Thanks for feed!

brycewray avatar Apr 18 '22 18:04 brycewray