planetscale/database-js

Parse Date, timezone problem because Z is removed

roelandmoors opened this issue · 2 comments

There is a sanitiser that removes the Z from a date:

return quote(value.toISOString().replace('Z', ''))

But when I parse that string back to a date I get the wrong result. I needed to add the Z back to make it work:

new Date(Date.parse(d + "Z"));

I had that problem here:
https://github.com/roelandmoors/authjs-adapter-mysql/blob/5867a205154f91c88086c0ce2a5101e79c61faf7/src/db.ts#L45

Seems like a bug to me?

See #64.

I now have a Date to string conversion like below. This way the Z can be stripped without a problem.

export function datetimeToStr(d) {
  const tzoffset = d.getTimezoneOffset() * 60000; //offset in milliseconds
  return new Date(d.getTime() - tzoffset).toISOString().slice(0, -1);
}