简体   繁体   中英

how to change input date format

I have a input field which is date. It looks like this output image i got

It was showing dd/mm/yyyy in UI. But my need was yyyy/mm/dd . It should show yyyy/mm/dd instead of dd/mm/yyyy

Code I tried:

export default function App() {
const onDateChange = (dateValue) =>{
console.log(dateValue)
}
return (
<div>
<input onChange = {(e)=>{onDateChange(e.target.value)} } type="date" format='yyyy/mm/dd' />
</div>
);
}

My need was yyyy/mm/dd . It should show yyyy/mm/dd instead of dd/mm/yyyy I tried searching stackoverflow. Some answers shows, it changes depends upon the locale. But I need it to be yyyy/mm/dd everytime fixed. I tried a lot. But cant find solutions. Please help me with some solutions

For a better experience with dates, I'd recommend you to use the day.js library, it's quite small but very useful, in it you can find an option to format your dates as you wish eg dayjs().format(YYYY-MM-DD)

Hope it works for you

To set and get the input type date in dd-mm-yyyy format we will use type attribute. The type attribute is used to define a date picker or control field. In this attribute, you can set the range from which day-month-year to which day-month-year date can be selected from.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM