0

I'm using react-native-date-picker to show the date picker. Here is how I'm doing it:

...
    const [userUserDOB, setUserDOB] = useState(new Date());
...
                        <DatePicker
                            style={styles.dobPicker}
                            date={userUserDOB}
                            onDateChange={setUserDOB}
                            mode={'date'}
                            textColor={colors.darkBlue}
                        />
...

    dobPicker: {
        alignSelf: 'center'
    },

This should show date picker but instead as soon as the screen loads I get this error:

TypeError: props.date.getTime is not a function. (In 'props.date.getTime()', 'props.date.getTime' is undefined)

enter image description here

Chaudhry Talha
  • 7,231
  • 11
  • 67
  • 116
  • are you sure date has a compatible type with getTime? – Alec Jul 13 '21 at 07:41
  • @Alec you mean here `mode={'date'}`? I'm not doing anything with time, only want to show the date. – Chaudhry Talha Jul 13 '21 at 07:47
  • Sounds like DatePicker's onDateChange handler doesn't pass an actual date object to the callback (setUserDOB) but maybe a date string, like `"2021-07-13T08:00:00.000Z"`. – Lennholm Jul 13 '21 at 09:15
  • @Lennholm is there a way to check this? Because as soon as the `TextInput` comes `onFocus` I have a state `const [showDate, setDateShow] = useState(false);` which I'm changing ` { setDateShow(true) }} />` – Chaudhry Talha Jul 13 '21 at 09:38

3 Answers3

1

This works for me, check this and let me know

setDOB = (event, d) => {
        let a= d..getTime()
        console.log(a);
        this.setState({DOB:a})
    }
    
< DateTimePicker
    testID="dateTimePicker"
    value={new Date(2015, 0, 1)}
    mode="date"
    cancelBtnText="Cancel"
    customStyles={{
    format="YYYY-MM-DD"
    maximumDate= {new Date(2015, 0, 1)}
    minimumDate={new Date(1950, 0, 1)}
    is24Hour={true}
    display="default"
    onChange={this.setDOB}
/>
Gourav Goel
  • 63
  • 1
  • 7
0

firstly check the date in prop and then pass it from new Date() as new Date( props.date ).getTime() this will work for you i.a

Chaudhry Talha
  • 7,231
  • 11
  • 67
  • 116
Ch Umair
  • 21
  • 3
  • Hi Umair, welcome to stack overflow. Can you please elaborate on what you mean? As I don't have a date prop, I just want to show today's date in case there is no date selected that is why I'm initializing the `userUserDOB` with a new date and not using the time. – Chaudhry Talha Jul 13 '21 at 08:55
  • const [date, setNewdate] = useState( new Date() ) .... const setDate = (data) => { setNewdate(data); } .... .... this one is working for me – Ch Umair Jul 13 '21 at 10:27
0

try like this

{!!userUserDOB?.getTime && <DatePicker
                            style={styles.dobPicker}
                            date={userUserDOB}
                            onDateChange={setUserDOB}
                            mode={'date'}
                            textColor={colors.darkBlue}
                        />}
Nikhil bhatia
  • 1,297
  • 1
  • 8
  • 9