I have a simple date input component, i just stuck when change the value of it. Because it always print value "1970-01-01".
Somebody knows how to do it. would appreciate thanks.
Here is my codes:
import React, { useState } from 'react';
import moment from 'moment';
const DateInput = ({ className = style.defaultSize, onChange }) => {
const [value, setValue] = useState(moment().format('YYYY-MM-DD'));
const onChangeDate = (date) => {
const newDate = moment(date.timeStamp).format('YYYY-MM-DD');
setValue(newDate);
console.log(newDate); //always log "1970-01-01"
};
return (
<input
type="date"
value={value}
onChange={onChangeDate}
/>
);
};
export default DateInput;
date.timeStampshould bedate.target.valuein theonChangeDatefunction.const newDate = moment(date.target.value).format("YYYY-MM-DD");. 👉 SandBox: