You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The DateTime scalar (GraphQLDateTime) fails to convert unix timestamp, for example, 1695416400, produces a date like 1970-01-20T14:56:56.400Z. Since the Unix timestamp represents the number of seconds since January 1st, 1970 at UTC, and the Javascript Date() function's argument is in milliseconds, not seconds, the number of seconds needs to be multiplied by 1000.
const humanReadableDate = new Date(1695416400 * 1000);
// result 2023-09-22T21:00:00.000Z
I just found this same issue today. This is breaking all my bigint timestamps that I'm pulling from my DB. I had to manually add in the old package I was using so my timestamps would stop breaking everywhere.
Describe the bug
The DateTime scalar (GraphQLDateTime) fails to convert unix timestamp, for example, 1695416400, produces a date like 1970-01-20T14:56:56.400Z. Since the Unix timestamp represents the number of seconds since January 1st, 1970 at UTC, and the Javascript Date() function's argument is in milliseconds, not seconds, the number of seconds needs to be multiplied by 1000.
const humanReadableDate = new Date(1695416400 * 1000);
// result 2023-09-22T21:00:00.000Z
To Reproduce
1- define your scalars and type
2- then import and export these types in a resolver:
3- Now you have some event resolver that returns this data:
4- The start and date will come across as
1970-01-20T14:56:56.400Z
and not the correct date.This is because the Unix timestamps aren't serialized correctly here, below:
https://github.com/Urigo/graphql-scalars/blob/v1.22.2/src/scalars/iso-date/DateTime.ts#L36
This line should be:
return new Date(value * 1000); (this matches graphql-iso-date implementation as well)
Expected behavior
Unix timestamp: 1695301200
Actual: 1970-01-20T14:55:01.200Z
Expected: 2023-09-21T13:00:00.000Z
Unix timestamp: 1695330000
Actual: 1970-01-20T14:55:30.000Z
Expected: 2023-09-21T21:00:00.000Z
Unix timestamp: 1695387600
Actual: 1970-01-20T14:56:27.600Z
Expected: 2023-09-22T13:00:00.000Z
Unix timestamp: 1695416400
Actual: 1970-01-20T14:56:56.400Z
Expected: 2023-09-22T21:00:00.000Z
Environment:
Additional context:
The text was updated successfully, but these errors were encountered: