简体   繁体   English

如何更改输入日期格式

[英]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.它在 UI 中显示dd/mm/yyyy But my need was yyyy/mm/dd .但我的需要是yyyy/mm/dd It should show yyyy/mm/dd instead of dd/mm/yyyy它应该显示yyyy/mm/dd而不是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 .我的需要是yyyy/mm/dd It should show yyyy/mm/dd instead of dd/mm/yyyy I tried searching stackoverflow.它应该显示yyyy/mm/dd而不是dd/mm/yyyy我尝试搜索 stackoverflow。 Some answers shows, it changes depends upon the locale.一些答案显示,它的变化取决于语言环境。 But I need it to be yyyy/mm/dd everytime fixed.但我需要它每次都固定为yyyy/mm/dd 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)为了更好地体验日期,我建议您使用 day.js 库,它很小但非常有用,您可以在其中找到一个选项来根据需要格式化日期,例如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.要以 dd-mm-yyyy 格式设置和获取输入类型日期,我们将使用 type 属性。 The type attribute is used to define a date picker or control field. type 属性用于定义日期选择器或控制字段。 In this attribute, you can set the range from which day-month-year to which day-month-year date can be selected from.在该属性中,可以设置从日月年到日月年日期可以选择的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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