react-datepicker icon indicating copy to clipboard operation
react-datepicker copied to clipboard

How to exclude time from the past (minTime), only if selected date is current date (minDate) ?

Open JustFly1984 opened this issue 9 years ago • 31 comments

I found how to exclude dates from the past with minDate, I just pass minDate={moment()}

If I set minTime={moment().hours().minutes()} it will exclude this time from all the dates.

I need to limit user to set time in past of current day.

I look for examples on the website, but could not find proper example.

PS. I Appreciate all the work you do for the community!

JustFly1984 avatar Nov 11 '17 10:11 JustFly1984

@martijnrusschen please have a look at this feature request!

manishbhatt94 avatar Dec 14 '17 08:12 manishbhatt94

I am looking for this feature too. I want to set the minTime to be 6 hours in the future.

I tried this minTime={moment().add(6, "hours")}, but it didn't seem to do what I was expecting.

mathiasha avatar Mar 23 '18 10:03 mathiasha

Have you tried passing minTime based on the selected date?

aij avatar Mar 23 '18 11:03 aij

I'm coming across this as well. Any ideas how to manipulate the value of the time if the user didn't select one? It doesn't seem to care what the 'minTime' is, it will always default to 12:00am.

justinkahrs avatar Apr 30 '18 20:04 justinkahrs

This seems like a bug at this moment. Per the documentation, one needs to set both minTime and maxTime. But doing so disables all the time fields.

const now = moment().tz('America/New_York')
...
        <DatePicker
          isClearable
          minDate={now}
          minTime={now.hours(now.hour()).minutes(now.minutes())}
          maxTime={now.hours(23).minutes(45)}
          onChange={handleChange}
          placeholderText="Click to select a date"
          selected={input.value ? moment(input.value, 'America/New_York') : null}
          todayButton="Today"
          showTimeSelect
          timeFormat="HH:mm"
          timeIntervals={15}
          dateFormat={format}
        />

Today's date image

Tomorrow's date image

mrchief avatar Jul 06 '18 15:07 mrchief

I would create a function that utilizes the excludeTimes parameter combined with your onChange event and/or selected parameter. If selected date === today's date then your excludeTimes will be an array of interval times you do not want displayed for this date. This function would have to run every time a date is selected on your onChange event and return null or and empty array when any other date other than the desired date is selected.

Note: This is untested but should in theory work. šŸ˜„

michaelmcshinsky avatar Jul 19 '18 15:07 michaelmcshinsky

+1 on this bug, same issue for me.

GavinThomas1192 avatar Jul 19 '18 21:07 GavinThomas1192

I solved this issue by rounding the minTime and maxTime to the nearest timeIntervals slot. I think @mrchief 's issue is that the now time is not one of the timeIntervals (example: if now = "8:43" it's not in the time intervals [8:45, 9:00])

Example:

// Rounds datepicker's time to the nearest 15 minute interval
round = (date, duration, method) => {
  duration = duration || moment.duration(15, 'minutes');
  method = method || 'floor';
  return moment(Math[method]((+date) / (+duration)) * (+duration));
}

render() {
  const startDate = this.state.startDate;
  const now = moment();

  const startDPConf = {
    minDate: now,
    showTimeSelect: true,
    timeFormat: "HH:mm",
    timeIntervals: 15,
    dateFormat: format
  }

  if (startDate.isSame(now, 'day')) {
    startDPConf.minTime = this.round(now, 15, 'ceil');
    startDPConf.maxTime = this.round(now.clone().endOf('day'));
  }

  <DatePicker {...startDPConf} />
}

fabiosangregorio avatar Oct 13 '18 08:10 fabiosangregorio

@fabiosangregorio The issue is that if you choose another date, the time is sticky. So if it's 1 pm, you can't see dates before 1 pm. Not only today but everyday.

Still a bug with no solutions on here

charlesmarino avatar Oct 23 '18 06:10 charlesmarino

@charlesmarino you can check if the selected date is the same of today, and if that is the case you can set the min/max time. I updated the previous comment with the code to do that.

fabiosangregorio avatar Oct 23 '18 12:10 fabiosangregorio

@fabiosangregorio Hi! This is not worked for me ((

Valerika avatar Feb 15 '19 16:02 Valerika

@Valerika here's a quick demo of it https://codepen.io/fabiosang/pen/PVyMJd?editors=0011

fabiosangregorio avatar Feb 15 '19 19:02 fabiosangregorio

@fabiosangregorio Thanks!!!!!!!!!!

Valerika avatar Feb 15 '19 19:02 Valerika

@fabiosangregorio (((( image

and no open DatePicker

Valerika avatar Feb 15 '19 20:02 Valerika

image

Valerika avatar Feb 15 '19 20:02 Valerika

image

state = { startDate: moment() }

Valerika avatar Feb 15 '19 20:02 Valerika

@fabiosangregorio This is due to the version, I lowered the version to 1.4.1 and it works Thanks!

Valerika avatar Feb 15 '19 22:02 Valerika

Happy to announce that I sorted this out in #1718 šŸš€

Min Date

Screen Shot 2019-04-21 at 16 28 25

Max Date

Screen Shot 2019-04-21 at 16 28 44

Read more details on the PR page.

PaulRBerg avatar Apr 22 '19 10:04 PaulRBerg

Hi guys, for @JustFly1984 's question, i just encountered , any solution?

dlrandy avatar May 16 '19 02:05 dlrandy

@dlrandy Me and my team has made custom React datepicker with better accessibility support, written in typescript, supporting all main features of react-datepicker, and I’m planning to make it as npm package this month. Does anybody want to join as maintainer? I already have pretty much successful npm project @react-google-maps/api, so I’m retry sure in success of new datepicker. You can see an implementation in production at https://www.drivefromto.com

JustFly1984 avatar May 16 '19 12:05 JustFly1984