Successfully added
React
by Patrik
Converting Local Input to UTC for API Submission
When a user selects a datetime in a <input type="datetime-local">
, the returned value (e.g., "2025-05-27T22:03"
) is in local time. To maintain UTC consistency on the backend, this must be converted to UTC.
Implementation:
const handleChange = (e) => {
const local = new Date(e.target.value); // local time
const utc = local.toISOString(); // UTC string for API
setPost({ ...post, created: utc });
};
This ensures accurate time data regardless of the user's timezone.
react
date conversion
utc
form submission
datetime
Referenced in:
Comments