Javascript 如何将物料UI日期时间选择器默认为当前UTC日期/时间
我正在尝试为用户创建一个对话框,用React和Material UI提供开始和结束日期/时间。我想将起始日期默认为当前UTC的开始日期,将结束日期默认为当前UTC的结束日期,但我甚至不知道如何获取显示的默认日期和时间 我的起点和终点是这样的:Javascript 如何将物料UI日期时间选择器默认为当前UTC日期/时间,javascript,reactjs,material-ui,Javascript,Reactjs,Material Ui,我正在尝试为用户创建一个对话框,用React和Material UI提供开始和结束日期/时间。我想将起始日期默认为当前UTC的开始日期,将结束日期默认为当前UTC的结束日期,但我甚至不知道如何获取显示的默认日期和时间 我的起点和终点是这样的: state = { start: new Date(), end: new Date() }; 然后在render方法中有字段: <TextField type="datetime-local" label="Sta
state = {
start: new Date(),
end: new Date()
};
然后在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}
/>
但是当我打开对话框时,文本字段有mm/dd/yyyy-:--
而不是在状态中设置的当前日期/时间。我已经尝试了ToutString和toString使用defaultValue或value提供日期,但没有任何效果
我希望当前UTC日期显示在文本字段中。这就是我的结论:
<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}
/>
物料界面不包括开箱即用的日期/时间选择器,文本字段默认为基础HTML 5输入。如果你想在所有平台上保持一致的行为,请使用类似的方法。@JaredSmith谢谢你-看起来很有趣!!!我对这一切都不熟悉,所以需要一些时间来弄清楚时刻、日期fns、luxon和dayjs,以便使用它。但这肯定比我现在做的要好。