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);
==== edit ====
the log of
console.log(formProps.date.toLocaleString());
console.log(typeof(formProps.date));
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 :/ )