简体   繁体   English

如何默认为Material UI日期时间选择器的当前UTC日期/时间

[英]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.

相关问题 物料UI时间选择器UTC - Material UI Time Picker UTC 如何从角形材料日期选择器中获取当前时间? - How to I get current time from angular material Date picker? 如何在jQuery UI日期/时间选择器中设置当前值? - How to set current value in jQuery UI date/time picker? 在 Material UI 库中以编程方式关闭日期/时间选择器? - Close Date/Time Picker programmatically in Material UI Library? 如何根据当前输入值在Bootstrap 3日期/时间选择器中设置默认日期,以便在首次使用时显示 - How to set default date in Bootstrap 3 Date/Time Picker based on current input value so it appears on first use 如何在 material-ui 中为当前日期和时间的文本字段类型 datetime-local 设置默认值? - how can I set the default value to the textfield type datetime-local in material-ui for the current date and time? jQuery Ui日期时间选择器 - Jquery Ui date time picker 在日期时间选择器中为当前时间增加4个小时 - Add 4 hours for current time in date time picker 以毫秒为单位将 Javascript 当前日期时间转换为 UTC 日期时间 - Convert Javascript current date time to UTC date time in milliseconds 如何在时间选择器中添加当前时间作为默认时间? - How to add current time as default in time picker?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM