react-native-ui-datepicker icon indicating copy to clipboard operation
react-native-ui-datepicker copied to clipboard

the 'date' param of 'onChange' is not a javascript Date object

Open YaoHuiJi opened this issue 1 year ago • 5 comments

Would returning a standard JavaScript Date type instead of dayjs object be more appropriate? Otherwise, it could be unintuitive and inconvenient for users who don't use the dayjs library(they have to use param.date.toDate() which is not mentioned in the document)

And I also found that when set timePicker=true, the param.date of onChange is a string? These behaviors are inconsistent, unintuitive and inconvenient. Is it designed this way for some reason?

YaoHuiJi avatar Jun 01 '24 12:06 YaoHuiJi

@YaoHuiJi I had the same thought, I want to use this library but the only thing turning me away is the dependency on DaysJS. The standard Date object in Javascript should be the date object used.

thecodecafe avatar Jun 02 '24 16:06 thecodecafe

It would be interesting if the onChange output was a javascript Date object, or at least sent as another parameter.

Can the project maintainers discuss this? It would be great to hear their opinion on this.

gustavo-bonfim avatar Jun 20 '24 22:06 gustavo-bonfim

It seems to be possible to use the Date object with this little workaround

function DatePickerModal({
	modal: { getParam },
}: ModalProps<"DatePickerModal">) {
  const defaultDate = getParam("date");
  const [selectedDate, setSelectedDate] = useState(defaultDate ?? new Date());

  return (
     <DefaultContainer variant="modal">
	<DateTimePicker
            onChange={({ date }) => {
              setSelectedDate(new Date(date as string));
            }}
           date={selectedDate}
           mode="single"
           locale="pt-br"
         />

       <Button>Confirmar</Button>
    </DefaultContainer>
    );
}

export default DatePickerModal;

gustavo-bonfim avatar Jun 20 '24 22:06 gustavo-bonfim

couldnt you just do onChange={(params: {date: DateType}) => setDate(params.date as Date)}? Works for me

ronKovler avatar Oct 04 '24 04:10 ronKovler

@ronKovler that won't be a good idea as DateType and Date are not the same object, you would instead need to fully convert DateType to a Date by instantiating Date with a string representation of the full date from DateType.

thecodecafe avatar Oct 04 '24 10:10 thecodecafe

Using TypeScript, a safe way to get a Date object is to do the following

onChange={({ date }) => {
  if (date && typeof date === "object" && "toDate" in date) {
    const d = date.toDate();
    console.log("change", d);
  }
}}

This should be in doc for sure.

MoOx avatar Nov 05 '24 08:11 MoOx

Yes, you are right it returns the dayjs object I will change it to the javascript Date object in the next release. in some days 👀

farhoudshapouran avatar Feb 18 '25 18:02 farhoudshapouran

Hi again after v3.0.3 all onChange methods single, range, and multiple return javascript Date object.

farhoudshapouran avatar Feb 19 '25 07:02 farhoudshapouran

@YaoHuiJi I had the same thought, I want to use this library but the only thing turning me away is the dependency on DaysJS. The standard Date object in Javascript should be the date object used.

after v3 you don't need to install dayjs anymore if you don't need it.

farhoudshapouran avatar Feb 19 '25 08:02 farhoudshapouran

@farhoudshapouran onChange will still return all possible types?

https://github.com/farhoudshapouran/react-native-ui-datepicker/blob/f3b5d1b2a88472098063cda7a9095d1b41e86e0b/src/types.ts#L6

Grohden avatar Feb 19 '25 19:02 Grohden

@farhoudshapouran Any updates on this? Is it safe to assume the onChange parameter will be of type Date

TheAlexDev23 avatar Apr 25 '25 08:04 TheAlexDev23