question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow only times in the future

See original GitHub issue

I would like to set a restriction on the possible times which can be selected. Only times in the future should be possible.

On today’s day (2020-03-01 17:13) I want no dates from the past, and no hours before 18:00 to be selectable.

On every future day, all possible times should be selectable.

I can get the part with the dates working by setting minDate, but I can’t see any option for the time restriction.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8

github_iconTop GitHub Comments

4reactions
GeorgeWLcommented, Mar 9, 2020
minDate={moment(new Date()).startOf('day').toDate()}

the equivalent can be done with dayjs, or through native functions.

3reactions
Valerikacommented, May 11, 2020

@mindhaq This code is working. I’m using this component for resolving this issue.

import React, { Component } from "react";
import DatePicker from "react-datepicker";
import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";

dayjs.extend(duration);

export default class MyDatePicker extends Component {
  constructor(props) {
    super(props);
    this.state = {
      startDate: new Date()
    };
  }

  handleChange = (date) => {
    const { onChangeInput } = this.props;
    this.setState({
      startDate: date
    });
    const isoDate = dayjs(date || 0).format("YYYY-MM-DDTHH:mm:ssZ");
    onChangeInput(isoDate);
  };

  round = (date, duration, method) => {
    method = method || "floor";
    return new Date(Math[method](+date / +duration) * +duration);
  };

  render() {
    const { labelText, id, selectedParent, value } = this.props;

    const now = new Date();
    const startDate = this.state.startDate;

    const startDPConf = {
      minDate: now,
      showTimeSelect: true,
      timeFormat: "HH:mm",
      selected: Date.parse(startDate),
      timeIntervals: 15
    };

    if (dayjs(startDate).isSame(dayjs(now), "day")) {
      startDPConf.minTime = this.round(dayjs(now).valueOf(), 15, "ceil");
      startDPConf.maxTime = this.round(dayjs(now).endOf("day").valueOf(), 15, "floor");
    }

    return (
          <DatePicker
            {...startDPConf}
            onChange={this.handleChange}
            onKeyDown={(e) => e.preventDefault(e)}
            id={id}
            {...this.props}
          />
    );
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to accept only future or current time in input type='time'?
Here I give an simple solution. If both value are less than current value it will alert the user. $(document).ready(function(){ ...
Read more >
Time travel into the future is totally possible - Phys.org
You will travel through time into the future. Nothing will feel different for you, but after a couple of years' journey you would...
Read more >
Is time travel possible? - Space.com
Science says time travel is possible. Here, we explore some of the theories behind time travel and the science that supports time-bending.
Read more >
The Real Rules for Time Travelers - Discover Magazine
Time travel may in fact be possible, but it wouldn't work like in Back to the Future. ... only to reappear many years...
Read more >
Display only future dates in Datepicker
Here is a complete solution where you can use <apex:input type="date" with jQuery Datepicker where property minDate set to 0 (to show only...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found