I have a date picker that I modified to only let the user select month/year. Now, I only want a part of the data(month/year) because it returns something like this
"Thu Feb 18 2010 21:11:54 GMT+0800 (China Standard Time)".
I am thinking of putting the whole string in the array and get the indexes of the month and year but before I do that, I want to know if there is a much better and faster way.
Now, what I'm doing is like this
const [selectedDate, setSelectedDate] = useState(new Date());
const handleDateChange = (date) => {
console.log(date);
};
<DatePicker
variant="inline"
openTo="year"
views={["year", "month"]}
label="Year and Month"
value={"selectedDate"}
onChange={handleDateChange}
/>
It logs "Thu Feb 18 2010 21:11:54 GMT+0800 (China Standard Time)". I only want the "Feb 2010".
To store the date as you have detailed in the selectedDate variable, change the handleDateChange function to this.
const handleDateChange = (date) => {
setSelectedDate(new Date(date.getFullYear(), date.getMonth()));
}
let dateObj = new Date();
For local Date:
console.log((dateObj.getFullYear()) + "/" + (dateObj.getMonth() + 1));
For Universal:
console.log((dateObj.getUTCFullYear()) + "/" + (dateObj.getUTCMonth() + 1));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.