Search code examples
reactjsmomentjsantddate-fns

How to set a default value in antd datepicker with date-fns


I have replaced moment with date-fns for date-picker using this guide. But I am not able to set the default value in the date-picker with date-fns.

It is done with momentjs in the following way but I can't figure out how to do this with date-fns

const dateFormat = 'YYYY/MM/DD';
<DatePicker defaultValue={moment('2015/01/01', dateFormat)} format={dateFormat} />

Solution

  • After including the packages from the guide you linked to you would use the date-fns parse() method. You will need to update your dateFormat to 'yyyy/mm/dd'.

    const dateFormat = "yyyy/mm/dd";
    
    <DatePicker
       defaultValue={parse("2015/01/22", dateFormat, new Date())}
       format={dateFormat}
    />
    

    Here is a working sandbox