How to transform a Luxon DateTme object to a date
Asked Answered
F

1

15

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.

Feininger answered 18/1, 2021 at 13:41 Comment(0)
P
34

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>
Pacorro answered 18/1, 2021 at 13:47 Comment(5)
I already tried is saying is not a function as an error :(Feininger
@Feininger that's something that should be in the question. Otherwise people like VincenzoC waste their time writing up answers for this you've already tried...Runnel
I found that I was doing something wrong and the solution actually work now :)Feininger
...and you didn't tell anybody here what you were dong wrong and what the solution was. This is getting better and betterMegaton
but if i change the zone like this DateTime.fromISO(DateTime.now().toISO(), { zone: 'America/Edmonton', }).toJSDate(); ... this would give me localtimezone date objectFearless

© 2022 - 2024 — McMap. All rights reserved.