Home > Software design >  Unhandled Rejection (TypeError): formProps.date.getTime is not a function
Unhandled Rejection (TypeError): formProps.date.getTime is not a function

Time:11-10

How can I change hour and minute values to an existing Date variable?
formProps.date: existing Date type variable generated from a date picker that I want to use year value only.
formProps.hour: The hour value that user input separately.
formProps.minute: The minute value that user input separately.

Those three values are to be combined into a new Date variable 'dateWithTime', but it throws an error during copying the date values.

It this a wrong way to copy a Date variable? or is there any better way to make it?

const dateWithTime = new Date(formProps.date.getTime());
dateWithTime.setHours(formProps.hour, formProps.minute);

enter image description here

==== edit ====
the log of
console.log(formProps.date.toLocaleString());
console.log(typeof(formProps.date)); enter image description here

CodePudding user response:

I don't know why your code above doesn't work but the below code worked for me. Could u try to use:

const dateWithTime = new Date(formProps.date.toLocaleString());

(I know it is not an comprehensive answer but I couldn't add a comment due to my reputation :/ )

  • Related