Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. When I change the calendar date I'm getting an object with DateTime as follow:

Image of the DateTime OBJ

The code I'm using of the 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.

I'm doing a console.log(newDate) inside the handleDateOnChange and there is no more inside that why I'm not sharing that. The result of console.log() is the one you see above.

like image 232
Jakub Avatar asked Dec 11 '25 22:12

Jakub


1 Answers

You can simply use toJSDate()

Returns a Javascript Date equivalent to this DateTime.

const DateTime = luxon.DateTime;
const dt = DateTime.local();
console.log(dt);
console.log(dt.toJSDate());
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/global/luxon.min.js"></script>
like image 137
VincenzoC Avatar answered Dec 14 '25 11:12

VincenzoC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!