Search code examples
reactjsdaterangepicker

react datepicker default start date


 useEffect(() => {
    const loadAndSetData = async () => {
      setLoading(true);

      const startDate = moment()
        .startOf('day')
        .subtract(30, 'days');

      const endDate = moment().endOf('day');

      formRef.current.setFieldsValue({
        rangePicker: [startDate, endDate],
      });

      setLoading(false);
    };

  }, []);

Hi here is my code. I want to set start date as the first day of the previous month. I mean if today is 02.10.2021 start day should be 01.09.2021. How can i do that any idea?


Solution

  • You can use the subtract() and startOf() method to get this done.

    const startDate = moment().subtract(1,'M').startOf('month');
            console.log(startDate.toString());