Does any one know how to get only the year using the Textfield type date in react?
<TextField
key={"input-year"}
InputLabelProps={{ shrink: true }}
required
fullWidth
variant="outlined"
type="date"
label="Year"
margin="dense"
onChange={handleChange}
value={formValues?.year}
/>
2
Answers
You can do so using JavaScript’s string manipulation functions.In the handleChange function, event.target.value will give you the selected date in "YYYY-MM-DD" format. By using split(‘-‘), you can split this string into an array where the first element is the year.
You can use getFullYear method to get the year from selected event.