简体   繁体   中英

How to make react js DateTimePicker to auto display today's date OnChange?

I am very new to React JS. I currently have a web page with the a simple DateTimePicker. It works fine. Currently, the datetimepicker shows up as blank, and you can select a date from the dropdown, it'll populate the input and display this date. So what I want to do is instead if having it blank, I want to set the input to today's date right away, so that when I go to the page the datetimepicker's input is not blank - it already displays today's date. I tried a lot of things but they didnt work, in my code below I tried to use setDate(), but it didn't work. I also tried setDate(new Date()), which also didn't work...

function MyDateFunction(props) {
    const [canViewDatePicker, setCanViewDatePicker] = useState(
        !!props.user.accountExpiryDate
    );
    const { t } = useTranslation();

    return (
        <Field>
            <HorizontalFlex>
                <DateTimePicker
                    cssClassModifiers={['column', 'is-one-third']}
                    time={false}
                    disabled={!canViewDatePicker}
                    label={t('myLabel')}
                    Date={('setDate', '0')}
                    
                />
            </HorizontalFlex>
        </Field>
    );
}

The DateTimePicker that I am using is from react-widgets/lib package

Any ideas much appreciated:)

Yay i figured it out! Inside my <DateTimePicker/> I had to put defaultValue={new Date()}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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