简体   繁体   English

如何将 Luxon DateTme object 转换为日期

[英]How to transform a Luxon DateTme object to a date

I'm building a DatePicker React with the Material-UI picker library and using Luxon as an adapter.我正在使用 Material-UI 选择器库并使用 Luxon 作为适配器构建 DatePicker React。 When I change the calendar date I'm getting an object with DateTime as follow:当我更改日历日期时,我得到一个带有 DateTime 的 object 如下:

DateTime OBJ 的图像

The code I'm using of the DatePicker:我使用的 DatePicker 的代码:

<MuiPickersUtilsProvider utils={LuxonUtils}>
              <DatePicker
                className={classes.input}
                disableToolbar
                variant="inline"
                label="Date"
                format="cccc, LLLL dd"
                helperText=""
                value={date}
                margin="normal"
                onChange={newDate => {
                  handleDateOnChange({ newDate });
                  setDate(newDate);
                }}
                inputVariant="filled"
                fullWidth
                minDate={new Date()}
              />
            </MuiPickersUtilsProvider>

The `on change is giving me back the OBJ I shared in the screenshot and what I would like to get is the date. `on change 将我在截图中分享的 OBJ 还给我,我想要得到的是日期。

I'm doing a console.log(newDate) inside the handleDateOnChange and there is no more inside that why I'm not sharing that.我在handleDateOnChange 里面做了一个console.log(newDate) handleDateOnChange里面没有更多我不分享它的原因。 The result of console.log() is the one you see above. console.log() 的结果就是您在上面看到的结果。

You can simply use toJSDate()您可以简单地使用toJSDate()

Returns a Javascript Date equivalent to this DateTime.返回与此 DateTime 等效的 Javascript 日期。

 const DateTime = luxon.DateTime; const dt = DateTime.local(); console.log(dt); console.log(dt.toJSDate());
 <script src="https://cdn.jsdelivr.net/npm/luxon@1.25.0/build/global/luxon.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM