I have not use any datepicker still code is working fine. 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>
Edit:
Issue solve by other way
I used React Date Picker
It is so easy to implement. Just install the npm package
npm install react-datepicker --save
Install moment aslo
npm install moment --save
Import the libraries
import DatePicker from 'react-datepicker';
import moment from 'moment';
import 'react-datepicker/dist/react-datepicker.css';
In the constructor
this.state = {
dob: moment()
};
this.dateChange = this.dateChange.bind(this);
dateChange function
dateChange(date) {
this.setState({
dob: date
});
}
And finally the render function
<DatePicker
selected={this.state.dob}
onChange={this.dateChange}
className="form-control"
placeholder="Date of Birth"
maxDate={new Date()}
/>
Here the maxDate function is used to disable future dates.
maxDate={new Date()}
Source: https://www.npmjs.com/package/react-datepicker
Thank you!
You can use a min, max attribute to restrict the date selection within a date range
<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.
Try using minDate as well for minimum date selection.
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.
now for check date is future you can use diff function of moment.js.
// 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
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.
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}/>
<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/
import moment from 'moment';
<
Input type="date"
max={moment().format("DD-MM-YYYY")}
onChange={(e) => setDOB(e.target.value)}
/>
PS: Ignore the classes
Use the Date class to get the current date and then use the max prop and give it a value of current date.
const DatePicker = () => {
let curr = new Date();
curr.setDate(curr.getDate());
let date = curr.toISOString().substring(0, 10);
return (
<div className="flex items-center text-center justify-center">
<div className="form-control">
<label className="input-group input-group-vertical">
<input type="date" className="input text-primary bg-neutral-focus font-bold input-group-sm" defaultValue={date} max={date} />
</label>
</div>
</div>
)
}
Related
I made a function which calculates difference between two dates, but i dont know how to take(?) data to it using inputs. I also tried to return diffInDays; and instead of that use this.setState({ diffInDays: diffInDays }). I've tried many combinations but none of them gave me the right result.
calculate(dateFrom, dateTo) {
if (!dateFrom || !dateTo) {
return null;
}
const arrival = moment(dateFrom);
const departure = moment(dateTo);
if (arrival.isAfter(departure)) {
return null;
}
const diffInDays = departure
.endOf('day')
.diff(arrival.startOf('day'), 'days');
return diffInDays;
}
those are the inputs I created:
this one is for the beginning
<div className="form-group">
<label htmlFor="dateFrom">Date From: </label>
<input
type="date"
className="form-control"
id="dateFrom"
onChange={this.onChangeDateFrom}
value={this.state.dateFrom}
name="dateFrom"
/>
</div>
and this one is for the end
<div className="form-group">
<label htmlFor="dateTo">Date to: </label>
<input
type="date"
className="form-control"
id="dateTo"
onChange={this.onChangeDateTo}
value={this.state.dateTo}
name="dateTo"
/>
</div>
and this is the one where I want to see the difference
<div className="form-group">
<label htmlFor="daysDiff">Days Difference: </label>
<input
type="number"
className="form-control"
id="daysDiff"
required
value={this.state.daysDiff}
name="daysDiff"
readOnly
/>
</div>
Picking the dates works fine. They also save in the MongoDB, the problem appears only with the difference of days.
You should use
departure.diff(arrival)
and for getting only day difference
departure.diff(arrival,'days')
I have a formsy form with a bunch of Inputs. I can see the value of those inputs in the onValidSubmit function. One example of such an input is the following:
<Input
name="myStartDate"
id="myStartDate"
value=""
label="Start Date (YYYYMM)"
type="tel"
validations={{
matchRegexp: /^(19|20)\d\d(0[1-9]|1[012])$/
}}
validationErrors={{
matchRegexp: 'Enter the year and month (YYYYMM).'
}}
placeholder="YYYYMM"
required
/>
In the formsy form I have:
onValidSubmit={this.onValidSubmit}
The function only logs the form data:
onValidSubmit(formData){
console.log(formData);
}
I want to change this input to be a react-datepicker component (DatePicker), however, after I make the necessary changes to make the DatePicker work, the "formData" object in the onValidSubmit no longer contains the value for "myStartDate". What should I do to make this work?
My DatePicker component looks like this: (it's basically the same as the Input but with some additional stuff)
<DatePicker
name="myStartDate"
id="myStartDate"
value=""
label="Start Date (YYYYMM)"
type="tel"
validations={{
matchRegexp: /^(19|20)\d\d(0[1-9]|1[012])$/
}}
validationErrors={{
matchRegexp: 'Enter the year and month (YYYYMM).'
}}
placeholder="YYYYMM"
required
placeholderText="ÅÅÅÅMM"
dateFormat="YYYYMM"
className="form-control"
selected={this.state.myStartDate}
onChange={this.myStartDate}
isClearable={true}
/>
I know that DatePicker handles dates using the moment.js and I will handle this change accordingly, however, as I said before, "myStartDate" no longer shows up in the "formData" object passed to "onValidSubmit". What gives?
Thank you very much for your help!
I have to format the date to DD-MMM-YYYY format. I Am using reactjs. The date is formatted in IE but not in Google Chrome. Please suggest.
<input name="requested_order_ship_date" type="date"
disabled={ this.state.mode }
value={ moment(this.state.item.requested_order_ship_date).format("DD-MMM-YYYY") }
className="form-control" onChange={ this.handleInputChange } />
This actually has nothing to do with React. for <input type="date"> values, Chrome expects the value of the date to be in YYYY-MM-DD format.
See Is there any way to change input type="date" format? for more info.
I am using react time picker(rc-time-picker) in my code, I want to set the time picker value dynamically.
Here is my Time Picker Code
<span id="editstartTime">
<span class="rc-time-picker timeStylstartTimeAdd">
<input type="text" class="rc-time-picker-input" readonly="" value="">
<span class="rc-time-picker-icon"></span>
</span>
</span>
And I tried like this.
$('#editstartTime span input').val(this.state.shifts[index].startTime);
But it did not worked for me.
Please help me to overcome this issue.
Thanks in advance.
TimePicker has a value state of type moment, which holds the current value.
so e.g. if you instantiate with a state called timePickerValue:
<TimePicker value={this.state.timePickerValue} ... />
then you can modify timePickerValue using
this.setState({timePickerValue: newValue})
(In your example newValue would be this.state.shifts[index].startTime)
I suggest you try out this example as it shows how to mutate the TimePicker.value state.
const now = moment().hour(0).minute(0);
<TimePicker
showSecond={false}
value={moment().hours(13).minute(1)}
className="time-picker"
placeholder="HH:MM"
onChange={(e) => {
console.log(e);
console.log(moment(taskCreateFormData.startTime));
onTimeChange(e, 'start');
}}
format={format}
use12Hours
/>
if you take format for use12Hours then if you write moment().hours(13).minute(1) time like this then it will convert to PM time format
I have two date time fields on reactjs form as
<div>
<input type="text" name="startdate" value={this.state.startDate} onBlur={this.validateStartDate} />
<input type="text" name="enddate"value={this.state.endDate} onBlur={this.validateEndDate}/>
</div>
I want to validate the startdate, whether it is less than endDate or not,
How to pass the endDate value to the validateStartDate function?
or
If there is a better way to validate the input date fields, please provide the same.
I see you're storing your endDate as a state. So, when the user blurs startDate call validateStartDate. In that function, use the endDate state value to validate.
See the pseudo code
<div>
<input type="text" name="startdate" value={this.state.startDate} onBlur={this.validateStartDate} />
<input type="text" name="enddate"value={this.state.endDate} onBlur={this.validateEndDate}/>
</div>
function validateStartDate(){
//check this.state.startDate < this.state.endDate
}
Or, you can use refs to get the endDate value.
<div>
<input type="text" ref="startDate" name="startdate" value={this.state.startDate} onBlur={this.validateStartDate} />
<input type="text" ref="endDate" name="enddate"value={this.state.endDate} onBlur={this.validateEndDate}/>
</div>
function validateStartDate(){
var startDate= ReactDOM.findDOMNode(this.refs.startDate).value
var endDate = ReactDOM.findDOMNode(this.refs.endDate).value
//check startDate < endDate
}
Hope this helps!
you can use refs supported by react.Check here for official documentation
<div>
<input type="text" name="startdate" ref= (input) => {this.startData = input.value; } value={this.state.startDate} onBlur={this.validateStartDate} />
<input type="text" name="enddate"value={this.state.endDate} onBlur={this.validateEndDate}/>
</div>
In validateEndDate function you can reference the ref.
validateEndDate(){
//you can use this.startDate here
}
NOTE: Refs do not work in stateless function components