最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - react-datepicker time selection with seconds - Stack Overflow

programmeradmin5浏览0评论

I need to choose seconds also from react date picker . I had gone through out the docs found this

In this solution i can select hour,min,AM/PM from this but no option for seconds is there any way to customize to select seconds also from this.help needed,below example (look for input Time)

I have tried by changing date format

dateFormat="MM/dd/yyyy h:mm:ss aa" not working

() => {
  const [startDate, setStartDate] = useState(new Date());
  return (
    <DatePicker
      selected={startDate}
      onChange={date => setStartDate(date)}
      timeInputLabel="Time:"
      dateFormat="MM/dd/yyyy h:mm aa"
      showTimeInput
    />
  );
};

I found the way to show seconds but this working fine but inside a model after we select time the whole Dialog is closing .I'm using this inside MaterialUi Dialog

() => {
  const [startDate, setStartDate] = useState(new Date());
  const ExampleCustomTimeInput = ({ value, onChange }) => (
    <input
      type="time"
      step="1"
      value={value}
      onChange={e => onChange(e.target.value)}
      style={{ border: "solid 1px pink" }}
    />
  );
  return (
    <DatePicker
      selected={startDate}
      onChange={date => setStartDate(date)}
      showTimeInput
      customTimeInput={<ExampleCustomTimeInput />}
    />
  );
};

I need to choose seconds also from react date picker . I had gone through out the docs found this

In this solution i can select hour,min,AM/PM from this but no option for seconds is there any way to customize to select seconds also from this.help needed,below example (look for input Time)

I have tried by changing date format

dateFormat="MM/dd/yyyy h:mm:ss aa" not working

() => {
  const [startDate, setStartDate] = useState(new Date());
  return (
    <DatePicker
      selected={startDate}
      onChange={date => setStartDate(date)}
      timeInputLabel="Time:"
      dateFormat="MM/dd/yyyy h:mm aa"
      showTimeInput
    />
  );
};

I found the way to show seconds but this working fine but inside a model after we select time the whole Dialog is closing .I'm using this inside MaterialUi Dialog

() => {
  const [startDate, setStartDate] = useState(new Date());
  const ExampleCustomTimeInput = ({ value, onChange }) => (
    <input
      type="time"
      step="1"
      value={value}
      onChange={e => onChange(e.target.value)}
      style={{ border: "solid 1px pink" }}
    />
  );
  return (
    <DatePicker
      selected={startDate}
      onChange={date => setStartDate(date)}
      showTimeInput
      customTimeInput={<ExampleCustomTimeInput />}
    />
  );
};
Share Improve this question edited Mar 9, 2021 at 6:29 Shaido 28.4k25 gold badges75 silver badges81 bronze badges asked Aug 3, 2020 at 8:59 DevDev 1896 silver badges13 bronze badges 1
  • Thanks! i was looking for the same – Maulik Sheth Commented Sep 23, 2020 at 20:13
Add a ment  | 

1 Answer 1

Reset to default 5

You can use the customTimeInput property and path own onChange function in it:

customTimeInput={
        <CustomTimeInput
          onChangeCustom={this.handleChangeTime}
        />
      }

Here is the ponent:

const CustomTimeInput = ({ date, onChangeCustom}) => {
  const value = date instanceof Date && !isNaN(date)
  ? // Getting time from Date beacuse `value` es here without seconds
    date.toLocaleTimeString('it-IT')
  : '';

  return (
    <input
      className="bp3-input bp3-fill"
      type="time"
      step="1"
      value={value}
      onChange={(event) => onChangeCustom(date, event.target.value)}
    />);
};

And callback sample:

handleChangeTime = (date, time) => {
  const [hh, mm, ss] = time.split(':');
  const targetDate = date instanceof Date && !isNaN(date) ? date : new Date();
  targetDate.setHours(Number(hh) || 0, Number(mm) || 0, Number(ss) || 0);
  this.handleDateChange(targetDate);
};
发布评论

评论列表(0)

  1. 暂无评论