DateInput should not include time information
What you were expecting:
Using DateInput I'd expect the string in the payload eventually posted to the API be in ISO date format: YYYY-MM-DD.
What happened instead:
Instead the value of the field includes time as well, for example: 2017-10-30T22:00:00.000Z
Steps to reproduce: Basic DateInput usage in http://www.material-ui.com/#/components/date-picker
Other information:
Due to this behaviour the component is not (easily) usable in the real world. A simple example:
Admin user has a local time UTC+2. Server works in UTC and expects all dates submitted either in ISO date, ISO datetime, or ISO datetime with timezone. Now when using the DateInput (with MU DatePicker) an admin user picks a date, let's say 2017-10-31. On server side the posted 2017-10-30T22:00:00.000Z is stored as 2017-10-30 (because time is irrelevant, it's a date input). Once the value is retrieved from the server, user sees 2017-10-30 in his DateField and is much confused.
There also seem to be no elegant way of solving this. Server either needs to know about users local timezone and make the needed corrections (an awful hack) or admin user has to specially pick a date with a UTC corrected time (something that cannot be done with DateInput at all).
Or am I missing something obvious here?
As a sidenote, I think the problem is not only in DateInput as MU DatePicker is already sending a Date object to DateInput component, which means fixing this thing is probably also a bad hack.
Environment
- Admin-on-rest version: 1.3.2
I may have found a workaround which could help some other poor souls tackling this. Docs describe Transforming input value, but give a faulty example (parser receives an ISO string, not Date obj).
const _tz_offset = new Date().getTimezoneOffset() / 60;
export const dateParser = v => {
const regexp = /(\d{4})-(\d{2})-(\d{2})/
var match = regexp.exec(v);
if (match === null) return;
var year = match[1];
var month = match[2];
var day = match[3];
if (_tz_offset < 0) {
// negative offset means our picked UTC date got converted to previous day
var date = new Date(v);
date.setDate(date.getDate() + 1);
match = regexp.exec(date.toISOString())
year = match[1];
month = match[2];
day = match[3];
}
const d = [year, month, day].join("-");
return d;
};
...
<DateInput parse={dateParser} source="date_start" label="Start date" />
Can you fix the doc too ? :)
This is fixed in the 2.0 branch
I need to come with a proper working solution for the docs. The current one I came up with is a bit too naive, it needs to handle day changes properly when it comes to month and year changes.
Edit: updated code sample above
Just so you know, the explanation in the PR#1455 using the parse={dateParser} is not suitable as it makes impossible to select some date times (let say 31 december at 11pm).
(At least for me with the Seoul +9:00 timezone)
@boutdemousse
Below code, It would work for you
And I Think It's better now. Converting UTC to GMT isn't just an extra day, it's is some time difference between regions.
const tzOffset = new Date().getTimezoneOffset() / 60
const dateParser = v => {
const regexp = /(\d{4})-(\d{2})-(\d{2})(T|\s)(\d{2}):(\d{2})/
let match = regexp.exec(v)
if (match === null) return
let year = match[1]
let month = match[2]
let day = match[3]
let hour = match[4]
let min = match[5]
if (tzOffset < 0) {
const date = new Date(v)
match = regexp.exec(date.toISOString())
if (!match) return null
year = match[1]
month = match[2]
day = match[3]
hour = match[4]
min = match[5]
}
const date = [year, month, day].join('-')
const time = [hour, min].join(':')
const result = date + 'T' + time + 'Z'
return result
}
<DateTimeInput
source="test"
parse={dateParser}
{...rest}
/>
How can i get timestamps on slecting date through DateInput?
@fullstacksk This should be asked on StackOverflow