#How to deal with Date inputs

1 messages · Page 1 of 1 (latest)

paper flower
#

Let's imagine this situation: A user selects a date (e.g. 2025-01-01) from a date input in UTC+4. Because the time is not important in this case, by default the time will be set to midnight (00:00 UTC+4).

From my understanding, the best practice here is the front-end should send the date as an ISO string in UTC (CMIIW). And, by default, JSON.stringify would convert the date object (2025-01-01 at 00:00 UTC+4) to an ISO string in UTC (2024-12-31T20:00:00.000Z).

Because I only want to know the date, I gave the field a data type of date.

Schema::create('foobar', function(Blueprint $table) {
    $table->date('date');
}

In the back-end, the ISO string would get converted to an instance of Carbon. And when the date is inserted to the database, the time is not stored, leaving only the date (2024-12-31 in UTC).

Now the problem is, when i want to get the date back, I would like to convert it back to UTC+4. But since the time is not stored in the database, when the date is converted back to Carbon instance the time would just be set to midnight (2024-12-31 at 00:00 UTC). So in the front-end when i convert it to UTC+4 the date would be (2024-12-31 at 04:00 UTC+4), which is not what I want (supposed to be 2025-01-01).

I know a couple of workarounds to solve this, but I'd like to know which one's best. Should I not send the date as an ISO string? Should I use datetime instead? or something else entirely?

For context, I'm using React, Inertia, and MariaDB as my database.

royal socket
#

You can use iso strings and then convert them to the browsers timezone on the frontend

#

If you're dealing with just dates then you have to make some assumptions and show it in UTC

paper flower
royal socket
#

Assume there's no timezone on it and don't transform it for that one...

golden urchin
#

just store datetime, and format it as a date outbound? If you want to adjust for timezone, you need the time as well. Otherwise there's nothing to adjust unless you presume a time of 00:00:00

paper flower