简体   繁体   English

如何为 Material-ui TextField Type=date 设置默认日期

[英]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.

相关问题 如何在 material-ui 中为当前日期和时间的文本字段类型 datetime-local 设置默认值? - how can I set the default value to the textfield type datetime-local in material-ui for the current date and time? 如何在 Material-UI TextField 中设置当前日期 - How Do I Set The Current Date In A Material-UI TextField ReactJS + Material-UI:如何使用 Material-UI 将当前日期设置为默认值<DatePicker> ? - ReactJS + Material-UI: How to set current date as default value using Material-UI's <DatePicker>? 默认日期在 Material UI 的 Textfield 中设置为当前日期,type="date" - Default date is being set to current date in Material UI's Textfield with type="date" 如何在 Material UI 中设置日期文本字段的值 - How to set value of a Date Textfield in Material UI 如何删除日历@material-ui/pickers 中的默认选定日期? - How to remove default selected date in Calendar @material-ui/pickers? 使用 React Material-UI 和 Formik,从服务器分配数据后 TextField 类型日期不会更新 - Using React Material-UI & Formik, TextField type date does not update after assign data from server 如何在 Material UI 中为 Textfield 类型日期设置“min”属性? - How can I set 'min' attribute for a Textfield type date in Material UI? Material ui Textfield type date, min 不工作 - Material ui Textfield type date, min not working 如何更改 material-ui 文本字段的默认类名? - How to change default className of the material-ui Textfield?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM