[英]how to set a default date for a Material-ui TextField Type=date
I just started working with React and I am trying to use a date picker using the material我刚开始使用 React,我正在尝试使用使用材料的日期选择器
it looks like this:它看起来像这样:
<TextField
name="someDate"
label="Some Date"
InputLabelProps={{ shrink: true, required: true }}
helperText={errors.someDate}
FormHelperTextProps={{ hidden: !this.hasError('someDate') }}
type="date"
error={this.hasError('someDate')}
onChange={this.handleSomeDateChange}
value={values.someDate}
/>
Setting: type="date" gives me the date picker but it also overlays the format for the default value to "dd/mm/yyyy".设置:type="date" 为我提供了日期选择器,但它还将默认值的格式覆盖为“dd/mm/yyyy”。 I want the default value to be values.someDate I've tried using the defaultValue prop and the value prop as shown above with no luck.
我希望默认值是 values.someDate 我已经尝试使用 defaultValue 道具和 value 道具,如上所示,但没有运气。 The only way to change the label is if I remove type="date" which also removes the datepicker.
更改标签的唯一方法是删除 type="date" ,这也会删除日期选择器。
How I can I set it to values.someDate on render?如何在渲染时将其设置为 values.someDate?
it looks like it should work with defaultValue:看起来它应该与 defaultValue 一起使用:
Please see it working here: https://codesandbox.io/s/9y6yz462op请在这里查看它的工作: https : //codesandbox.io/s/9y6yz462op
const values = {
someDate: "2017-05-24"
};
function App() {
return (
<div className="App">
<TextField
name="someDate"
label="Some Date"
InputLabelProps={{ shrink: true, required: true }}
type="date"
defaultValue={values.someDate}
/>
</div>
);
}
In my case I wanted to set my default Date to my current date.就我而言,我想将默认日期设置为当前日期。 So I took my current date and formatted it according to material textfield format.
所以我取了我当前的日期并根据材料文本字段格式对其进行了格式化。 So my default is always my current date.
所以我的默认值总是我当前的日期。
See in codeSandbox: https://codesandbox.io/s/elastic-sea-s7i76在 codeSandbox 中查看: https ://codesandbox.io/s/elastic-sea-s7i76
const dateNow = new Date(); // Creating a new date object with the current date and time
const year = dateNow.getFullYear(); // Getting current year from the created Date object
const monthWithOffset = dateNow.getUTCMonth() + 1; // January is 0 by default in JS. Offsetting +1 to fix date for calendar.
const month = // Setting current Month number from current Date object
monthWithOffset.toString().length < 2 // Checking if month is < 10 and pre-prending 0 to adjust for date input.
? `0${monthWithOffset}`
: monthWithOffset;
const date =
dateNow.getUTCDate().toString().length < 2 // Checking if date is < 10 and pre-prending 0 if not to adjust for date input.
? `0${dateNow.getUTCDate()}`
: dateNow.getUTCDate();
const materialDateInput = `${year}-${month}-${date}`; // combining to format for defaultValue or value attribute of material <TextField>
function App() {
return (
<form noValidate>
<TextField
id="date"
label="Birthday"
type="date"
defaultValue={materialDateInput} // Today's Date being used as default
InputLabelProps={{
shrink: true
}}
/>
</form>
);
}
Don't know if this is what you need.不知道这是不是你需要的。 But you can just manipulate the
new Date()
according to your need and this will work.但是您可以根据需要操作
new Date()
,这将起作用。
This should assist people will less experience in date.
这应该有助于人们在约会中体验较少。
对于可能会错过它的人, defaultValue
日期的格式是yyyy-mm-dd
,我使用的是dd-mm-yyyy
,但它没有呈现。
To clarify @bharat's answer, setting value for material ui input field requires that you format the date to be YYYY-MM-DD
.为了澄清@bharat 的答案,材料 ui 输入字段的设置值要求您将日期格式化为
YYYY-MM-DD
。 If you're using a library like dayjs
for example you can do this例如,如果您使用的是像
dayjs
这样的库,则可以执行此操作
import dayjs from 'dayjs';
<TextField
name="someDate"
label="Some Date"
InputLabelProps={{ shrink: true, required: true }}
helperText={errors.someDate}
FormHelperTextProps={{ hidden: !this.hasError('someDate') }}
type="date"
error={this.hasError('someDate')}
onChange={this.handleSomeDateChange}
value={dayjs(values.someDate).format("YYYY-MM-DD")}
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.