简体   繁体   English

"如何在 reactjs 中禁用未来日期?"

[英]How do I disable future dates in reactjs?

I have not use any datepicker still code is working fine.我没有使用任何 datepicker 仍然代码工作正常。 I have selected input type to date and everything's working fine.到目前为止,我已经选择了输入类型,并且一切正常。 Now I want to disable future dates.现在我想禁用未来的日期。 How do I do that?我怎么做?

<div className="form-group col-md-6">
                            <label for="inputDate4">Date of Birth</label>
                            <input type="date" className="form-control" id="inputDate4" placeholder="Date of Birth" name="dob" onChange={this.handleChange} />
                        </div>

You can use a min , max attribute to restrict the date selection within a date range您可以使用min , max属性来限制日期范围内的日期选择

<div className="form-group col-md-6">
     <label for="inputDate4">Date of Birth</label>
     <input type="date" className="form-control" id="inputDate4" placeholder="Date of Birth" name="dob" onChange={this.handleChange} max={moment().format("YYYY-MM-DD")}/>
</div>

The maxDate did the job for me. maxDate 为我完成了这项工作。 Try using minDate as well for minimum date selection.尝试使用 minDate 以及最小日期选择。

 import moment from 'moment'; import DatePicker from 'react-datepicker'; <DatePicker maxDate={moment().toDate()} />

var input = new Date ("inputgotBackfromthe user")    
var now = new Date();
if (before < now) {
  // selected date is in the past
}

You need to try converting the input to a date object, after that you can make a new date object and check the input date object is older or not.您需要尝试将输入转换为日期对象,然后您可以创建一个新的日期对象并检查输入的日期对象是否较旧。 Remember this will be big, because the users must input it in the right format.记住这会很大,因为用户必须以正确的格式输入它。 If not you will maybe get an exception or a wrong date object.如果不是,您可能会收到异常或错误的日期对象。

you can use moment.js library for working with date.您可以使用moment.js库来处理日期。 now for check date is future you can use diff function of moment.js .现在检查日期是未来你可以使用moment.js 的diff函数。

                   // today < future (31/01/2014)
today.diff(future) // today - future < 0
future.diff(today) // future - today > 0

Therefore, you have to reverse your condition.因此,你必须扭转你的状况。 If you want to check that all is fine, you can add an extra parameter to the function:如果你想检查一切是否正常,你可以向函数添加一个额外的参数:

moment().diff(SpecialTo, 'days') // -8 (days)

note:注意:

this won't work if it's a different day but the difference is less than 24 hours如果是不同的一天,这将不起作用,但差异小于 24 小时

I am checking the date selected by comparing it with the current date or precisely with moment() which gives us the value of current time elapsed in seconds from some date in 1970. It will not allow to select any future dates.我正在检查选择的日期,方法是将其与当前日期或与 moment() 进行比较,它为我们提供了从 1970 年某个日期开始以秒为单位的当前时间值。它不允许选择任何未来日期。

handleDateChange(date){
    if(date <= moment()){
        this.setState({
            testDate: date
        });
    }
}

<DatePicker className="form-control" dateFormat="DD/MM/YYYY" id="testDate onChange={this.handleDateChange.bind(this)} onSelect={this.handleSelect}                                                                selected={this.state.testDate}/>

使用此方法并在功能组件 ( https://i.stack.imgur.com/0Nyrj.jpg ) 上的 react js 中的输入字段中禁用过去日期

this works for me                       
 <DateTimePicker 
    dateConvention={DateConvention.Date}
    showGoToToday={true}
    
     onChange={date => this.handleDateChange(date)} 
     placeholder={"Enter Date"}
     showLabels={false} 
     value={this.state.startDate} 
     maxDate={this.props.todaydate}//or use new date()
     //formatDate={this._onFormatDate}                  
  />      
      check this -https://pnp.github.io/sp-dev-fx-controls-react/controls/DateTimePicker/
                        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM