[英]How to default to current UTC date/time for Material UI date time picker
I'm trying to create a dialog for the user to provide start and end date/time with React and Material UI. 我正在尝试为用户创建一个对话框,以使用React和Material UI提供开始和结束日期/时间。 I'd like to default the start to the beginning of the current day and the end to the end of the current day (UTC), but I can't even figure out how to get a default date and time to show up.
我想将开始默认为当天的开头和结束到当天结束(UTC),但我甚至无法弄清楚如何获得默认的日期和时间来显示。
I have the start and end in the state like this: 我有这样的状态的开始和结束:
state = {
start: new Date(),
end: new Date()
};
and then I have the fields in the render method: 然后我在render方法中有字段:
<TextField
type="datetime-local"
label="Start Date/Time"
InputLabelProps={{
shrink: true
}}
required={true}
// TODO make it start of today
defaultValue={this.state.start.toUTCString()}
onChange={this.handleStartChange}
/>
<TextField
type="datetime-local"
label="End Date/Time"
InputLabelProps={{
shrink: true
}}
required={true}
value={this.state.end.toString()}
// TODO make it end of today
onChange={this.handleEndChange}
/>
but when I open the dialog, the text fields have mm/dd/yyyy --:-- --
instead of the current date/time set up in the state. 但是当我打开对话框时,文本字段有
mm/dd/yyyy --:-- --
而不是状态中设置的当前日期/时间。 I've tried both toUTCString and toString to provide the date using defaultValue or value, but nothing is working... 我已尝试使用toUTCString和toString来使用defaultValue或value提供日期,但没有任何工作......
I'd like the current UTC date to show up in the text fields. 我希望当前的UTC日期显示在文本字段中。
this is what I ended up with: 这就是我最终得到的结果:
<TextField
style={{ margin: 0, width: '200px' }}
label="Start Date/Time (UTC)"
type="datetime-local"
defaultValue={this.state.start
.toISOString()
.slice(0, 16)}
InputLabelProps={{
shrink: true
}}
required={true}
onChange={this.handleStartChange}
/>
<TextField
style={{ margin: 0, width: '200px' }}
label="End Date/Time (UTC)"
type="datetime-local"
defaultValue={this.state.end
.toISOString()
.slice(0, 16)}
InputLabelProps={{
shrink: true
}}
required={true}
onChange={this.handleEndChange}
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.