Coder Social home page Coder Social logo

Comments (20)

ciscoheat avatar ciscoheat commented on July 28, 2024 2

Excellent! I've just released 0.6.7, this was a good improvement. Thank you for all your help.

from sveltekit-superforms.

ciscoheat avatar ciscoheat commented on July 28, 2024 1

Thank you very much for your investigation, I'll do some experimentation, trying to make this consistent!

from sveltekit-superforms.

ciscoheat avatar ciscoheat commented on July 28, 2024 1

Yes, already added that. :) It should be fine, so I'll add this for the update tomorrow.

from sveltekit-superforms.

thelinuxlich avatar thelinuxlich commented on July 28, 2024 1

I tested your changes locally and it seems the date input is behaving normally, date comes back to the backend untouched, nice!

from sveltekit-superforms.

xmlking avatar xmlking commented on July 28, 2024 1

I had a use case where I have to deal with datetime in UTC formate in the backed (GraphQL/Postgres) but converted to local datetime in the browser.

I had to implemente DateInput component which is used here

dateProxy looks like correct way to implement this. Looking forward for this solution…

from sveltekit-superforms.

thelinuxlich avatar thelinuxlich commented on July 28, 2024 1

Just confirmed the bug has been fixed!

from sveltekit-superforms.

thelinuxlich avatar thelinuxlich commented on July 28, 2024

Another thing I noticed: when using z.date({coerce: true}) fields, on page.server.ts the date is parsed correctly, but on the client-side it applies the local timezone and messes it up

from sveltekit-superforms.

thelinuxlich avatar thelinuxlich commented on July 28, 2024

Made some changes to showcase this behavior, notice the server log with the birthdate value and then the same value on the browser: https://stackblitz.com/edit/sveltekit-superforms-bug-reporting-ek13fm?file=src/routes/+page.server.ts

from sveltekit-superforms.

thelinuxlich avatar thelinuxlich commented on July 28, 2024

Found the fix: https://stackblitz.com/edit/sveltekit-superforms-bug-reporting-ek13fm?file=src/routes/+page.server.ts

It's two things:

  • sveltekit-superforms should add the transform in the example above to any z.date() field
  • The function inside the transform to convert date to UTC should also be applied to dateProxy (which just executes new Date(date)), otherwise you get the right date on load but whenever you change the date on the input it messes it up again.

from sveltekit-superforms.

ciscoheat avatar ciscoheat commented on July 28, 2024

@thelinuxlich Could you clarify what was the problem you had? I've tested with a fork of your project: https://stackblitz.com/edit/sveltekit-superforms-bug-reporting-91avkz

I've commented out the date transform just for testing, then changed dates and submitted several times, but I cannot see that there is a discrepancy between any dates. They are the same both on server and on client.

from sveltekit-superforms.

thelinuxlich avatar thelinuxlich commented on July 28, 2024

You need to change the locale/region of your system. For example, I'm in Sao Paulo - Brazil, which is timezone GMT -03:00. On the server the timezone doesn't get applied, it's UTC. On the browser it does, so a UTC date becomes 1 day before because of the -03:00.

from sveltekit-superforms.

ciscoheat avatar ciscoheat commented on July 28, 2024

I'm at +02:00, but you mean it happens when you are in a negative timezone?

from sveltekit-superforms.

thelinuxlich avatar thelinuxlich commented on July 28, 2024

yeah, because at +02:00 a UTC date like 1984-09-02T00:00:00.000Z won't change the day, right?

from sveltekit-superforms.

ciscoheat avatar ciscoheat commented on July 28, 2024

Yes, that will mess things up because there is only a "date-local" format. I was considering adding a utc format as well, but was hoping it wouldn't be needed. Here's to hoping... :)

When adding that format, it seems to work, though I'm not sure if "date-utc" is semantically correct. It should really mean "use the date part, no matter what timezone", since the date input is a string value, and converting it to date may actually be problematic?

from sveltekit-superforms.

thelinuxlich avatar thelinuxlich commented on July 28, 2024

I think date-utc is correct, while you are at it, may add datetime-utc for datetime fields too.
I don't think the conversion would be problematic, the source code for the proxies has a toValue function already doing new Date(val)

from sveltekit-superforms.

thelinuxlich avatar thelinuxlich commented on July 28, 2024

I think you'll need to change this line to convert to UTC too when it's "date-utc": https://github.com/ciscoheat/sveltekit-superforms/blob/main/src/lib/client/proxies.ts#L100

Otherwise the z.date() field receives new Date("YYYY-mm-dd") and goes to the server with the local timezone

from sveltekit-superforms.

ciscoheat avatar ciscoheat commented on July 28, 2024

If you convert it to UTC at that point, which will happen when you change date in the input field for example, it will for negative timezones go backwards, making the input show one day earlier again.

I think the problem is that the date input doesn't care about time more than setting it to midnight at the current timezone. That's why I think describing the format as "date-utc" could be a bit misleading. Maybe a final option is to add just "date", that strips the rest of the ISO string?

from sveltekit-superforms.

thelinuxlich avatar thelinuxlich commented on July 28, 2024

Which is why I added the transform()

from sveltekit-superforms.

ciscoheat avatar ciscoheat commented on July 28, 2024

I have pushed the fixes now to the repo, not released a npm version yet. As it is now, if you instantiate the date as:

const date = new Date('1984-09-02');

And use the date (or date-utc) format, it works without any transform:

let proxy = dateProxy(form, 'proxy', { format: 'date' });

2023-04-03 18_43_54-localhost_5173_dates

Does this sound good, or can you find some case where it will be problem?

from sveltekit-superforms.

thelinuxlich avatar thelinuxlich commented on July 28, 2024

if the date remains 1984-09-02 after the date proxy sends the update to the $form field and back to the backend, then yeah it's solved

from sveltekit-superforms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.