Datetimepicker: Wrong value returned from picker

Created on 25 Jan 2020  路  10Comments  路  Source: react-native-datetimepicker/datetimepicker

Bug

Time picker returns today's date instead of given date.

In datetimepicker.android.js 53rd line should be const date = new Date(value); instead of const date = new Date();

Most helpful comment

Seeing the same issue. Thanks for finding the fix! Guess I'll shim it in until the package gets updated

If you don't want to edit package files, there's a fix (you need to use moment package):

`onChange = (event, date) => {
const { type } = event;

const chosenDate = moment(date).toObject();
const currentDate = moment(yourCurrentDateHere).toObject();

let newDate;

if ("set" === type) {
    if ("date" === mode) {
        newDate = moment({
            ...currentDate,
            years: chosenDate.years,
            months: chosenDate.months,
            date: chosenDate.date
        });
    }

    if ("time" === mode) {
        newDate = moment({
            ...currentDate,
            hours: chosenDate.hours,
            minutes: chosenDate.minutes,
            seconds: chosenDate.seconds,
            miliseconds: chosenDate.miliseconds
        });
    }
}

};`

All 10 comments

Seeing the same issue. Thanks for finding the fix! Guess I'll shim it in until the package gets updated

Seeing the same issue. Thanks for finding the fix! Guess I'll shim it in until the package gets updated

If you don't want to edit package files, there's a fix (you need to use moment package):

`onChange = (event, date) => {
const { type } = event;

const chosenDate = moment(date).toObject();
const currentDate = moment(yourCurrentDateHere).toObject();

let newDate;

if ("set" === type) {
    if ("date" === mode) {
        newDate = moment({
            ...currentDate,
            years: chosenDate.years,
            months: chosenDate.months,
            date: chosenDate.date
        });
    }

    if ("time" === mode) {
        newDate = moment({
            ...currentDate,
            hours: chosenDate.hours,
            minutes: chosenDate.minutes,
            seconds: chosenDate.seconds,
            miliseconds: chosenDate.miliseconds
        });
    }
}

};`

@NonameLTUs what about the initial date that the picker opens to. Is there any way to open it to the value passed to the value prop

Did you find a solution to this. I'm also facing this issue. Whenever I change the date it always returns today's date, kind of resets the date to today's date don't know why.

Did you find a solution to this. I'm also facing this issue. Whenever I change the date it always returns today's date, kind of resets the date to today's date don't know why.

Yep, look at the comment above. It works for me pretty well.

@NonameLTUs Thanks actually i was using moment already still was getting this issue. I actually had created a wrapper component over datepicker to include Done/Cancel button in ios. So in my case i was updating the state on change but my prop was not changing and that was resulting in reseting the picker. After figuring this out it was easy to fix.

Looks like a good solution, but is it possible to to the same thing but in the native lib also. On IOS it work perfectly, it seems odd to do extra work. It could take the date from the value option and call the setHour and setMinute that are already called on the onTimeChange

Alternatively, using moment as well. I was facing same problem, I just format the date using moment that I saw here #79. Then passed back the data using moment().toDate()

```
const {type} = event;
const mmtime = moment(date).format("YYYY-MM-DD HH:mm:ss");

if(type === "set"){
  if(this.state.mode === "date"){
    let dd = moment(date).format("YYYY-MM-DD");
    this.setState({
      mode: "time",
      pre_made_date: dd
    });


  }else{
    let tt = moment(date).format("HH:mm:ss");
    let combine = this.state.pre_made_date+"T"+tt;  
    this.setState({
      showDTpicker:false,
      pre_made_time: tt,
    });

    if(this.state.currentShowPicker === "from"){
      this.setState({
        from_date_time: moment(mmtime).toDate()
      });
    }else{
      this.setState({
        to_date_time: moment(mmtime).toDate()
      });
    }        
  }

}

```

Looks like a good solution, but is it possible to to the same thing but in the native lib also. On IOS it work perfectly, it seems odd to do extra work. It could take the date from the value option and call the setHour and setMinute that are already called on the onTimeChange

It's up to developers of the package ;)

This issue has not been fixed still. Using the latest v 2.3.0, On setting time, the date still changes to the current date.

Was this page helpful?
0 / 5 - 0 ratings