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.

DateRange for DatePicker

See original GitHub issue

I have two datepicker inputs and I want that user selects a date range.

this is the expected behavior: 1- user clicks on the first datepicker input and selects the first day. now the start day must have class -selectedStart start

2- user clicks on second datepicker input to select end day. this time datepicker have to keep the first value from first datepicker as ‘from’ and set new value as ‘to’. end

the question is how can I do this for two different datepicker (e.g. keeping selectedStart for second datepicker) and set selected value for each input? a similar implementation is on alibaba hotel datepicker

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Kiarash-Zcommented, Mar 22, 2020

@thebrodmann thank you for taking care of this issue, highly appreciate that 😊

1reaction
spostad7commented, Mar 21, 2020

thanks, @thebrodmann! this is final try on selecting a range from datepicker. at last, I format the input date and show the user only the ‘to’ value. for ‘from’ datepicker I used single date selection and for ‘to’ datepicker I used date range including an object that has ‘from’ and ‘to’ value.

export class SearchHotel extends PureComponent {
	constructor(props) {
		super(props);
		this.state = {
			dateRange: {
				from: null,
				to: null
			},
			fromDate: null,
			toDate: null
		};
	}

	

	onFromDateChange = date => {
		this.setState(prevState => ({
			fromDate: date,
			dateRange: {
				...prevState.dateRange,
				from: date
			}
		}));
	};

	onToDateChange = date => {
		if (!this.state.fromDate) {
			alertify
				.delay(4000)
				.closeLogOnClick(true)
				.error('لطفا ابتدا تاریخ ورود را انتخاب کنید');
			return false;
		}
		this.setState({
			fromDate: date.from,
			dateRange: date
		});
	};

	formatDateInput = () => {
		if (!this.state.dateRange.to) return '';
		const toDate = this.state.dateRange.to
		const year = toDate.year.toString();
		const month = toDate.month.toString().padStart(2, 0);
		const day = toDate.day.toString().padStart(2, 0);
		return `${year}/${month}/${day}`;
	};

	render() {
		const props = this.props;
		return (
			<form className={this.props.className} onSubmit={this.handleSearch}>
				
				<div
					className={this.props.className + '__fieldset mb-20 mt-20'}>
					<Label className={this.props.className + '__label'}>
						تاریخ ورود
					</Label>
					<DatePicker
						className={props.className}
						minimumDate={utils('fa').getToday()}
						value={this.state.fromDate}
						onChange={this.onFromDateChange}
						renderFooter={() => (
							<div
								style={{
									display: 'flex',
									justifyContent: 'center',
									alignItems: 'center'
								}}>
								<button
									type="button"
									onClick={() =>
										console.log(
											'today',
											this.state.persianToday
										)
									}
									className="btn DatePicker__footer-btn">
									امروز
								</button>
							</div>
						)}
					/>
				</div>
				<div className={this.props.className + '__fieldset mb-20'}>
					<Label className={this.props.className + '__label'}>
						تاریخ خروج
					</Label>
					<DatePicker
						className={props.className}
						minimumDate={utils('fa').getToday()}
						value={this.state.dateRange}
						onChange={this.onToDateChange}
						formatInputText={this.formatDateInput}
						renderFooter={() => (
							<div
								style={{
									display: 'flex',
									justifyContent: 'center',
									alignItems: 'center'
								}}>
								<button
									type="button"
									onClick={() =>
										console.log(
											'today',
											this.state.persianToday
										)
									}
									className="btn DatePicker__footer-btn">
									امروز
								</button>
							</div>
						)}
					/>
				</div>
			</form>
		);
	}
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Date Range Picker — JavaScript Date & Time Picker Library
A JavaScript component for choosing date ranges, dates and times. ... Originally created for reports at Improvely, the Date Range Picker can be...
Read more >
Date range picker on jquery ui datepicker - Stack Overflow
So I took the original answer and just made a few minor changes: In html, create a hidden input associated with the datepicker....
Read more >
Date Range Picker for Bootstrap
The Date Range Picker can be turned into a single date picker widget with only one calendar. In this example, dropdowns to select...
Read more >
Markup — bootstrap-datepicker documentation - Read the Docs
Using the input-daterange construct with multiple child inputs will instantiate one picker per input and link them together to allow selecting ranges. <div ......
Read more >
Date Range in JavaScript DatePicker control - Syncfusion
DatePicker provides an option to select a date value within a specified range by using the min and max properties. Always the min...
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