Dynamically change min, max date on datetime change event in AngularJs - angularjs

I'm using an existing directive to select start date and end date.
Requirement:
End Date always should be equal or greater than Start Date.
User may select start date as a future day or old day. min date of the End Date time picker should be set disabled based on start date selection.
Can anyone suggest me, how to set min date for end date dynamically based on start date?
start Date:
<input singledatepicker class="form-control" selected-date="startDateTime" placeholder="Date" format="DD/MM/YYYY" required readonly style="background:white;" max-date={{pastDate}}>
End Date:
<input singledatepicker class="form-control" selected-date="endDateTime" placeholder="Date" format="DD/MM/YYYY" required readonly style="background:white;" min-date={{IwantToSetThisValue}}>

From the documentation of daterangepicker, we can see that, there are options available to set
> minDate: (Date or string) The earliest date a user may select.
> maxDate: (Date or string) The latest date a user may select
but these values need to set while initializing the daterangepicker object. and u need to update this min date, according to the start date.
As this daterangepicker library not providing any methods to update the option on runtime, one quick solution is to reinitialize the 'end date picker' option with value of 'start date picker'.
You watch the start date model value and reinitilize the end date picker on the value change, something like this:
$scope.$watch('startDateTime', function (startDateValue) {
if (startDateValue) {
$('#endDateTime').daterangepicker({
minDate: startDateValue // Do corrections according to the requirements
});
}
});
Note: I'm not sure what your singledatepicker directive is doing, This is just a workaround, not the perfect way, But hope you got the idea.

Related

Unable to set initial custom date with Angular Moment Picker without validation tag

I am using the Angular Moment Picker library and trying to set a custom date in the input field whilst also trying to prevent the user from selecting a date in the past.
My element looks like so:
<input class="form-control"
moment-picker="endTimePicker"
name="campaignEndDate"
locale="en"
format="MMMM Do YYYY, h:mm A"
today="true"
start-view="month"
min-date="minDate"
ng-model="endTime"
ng-model-options="{ updateOn: 'blur' }"
ng-required="!campaign.NeverEnds"
style="width: 60%;"/>
And my controller:
$scope.minDate = moment();
$scope.endTime = moment( $scope.campaign.EndTime );
When I open the form, the Angular Moment Picker shows today's date/time in the input field, even though $scope.endTime is showing a date in the past.
The only way I can seem to get the correct date showing in the input field is to set validate="false" on the element, but then the control doesn't pass validation because the date is a date in the past.
What I am trying to achieve is to show an initial date (2020-07-29 02:14:00) when the user opens the form, but if they try and change the endTime, then cannot select a new date in the past.
Is this achievable somehow?
Okay, after playing around with this some more, and going back over the documentation, I found the best approach to achieve what I wanted was to implement a selectable method:
angular-moment-picker#methods
I removed the min-date="minDate" which allowed the Moment Picker to then show the correct date in the input field and then added a selectable="isSelectable( date )" to the element to filter out and disable any previous days:
$scope.isSelectable = function( date ) {
// Disable all previous days
return date >= moment();
};

How to pick multiple dates in one go with react-datepicker?

I know that there are other packages which help in picking multiple dates. But is there any way I can do this using react-datepicker only?
If you are referring to a date range you can use the following format,
<DatePicker
selected={this.state.startDate}
onChange={this.handleChange}
minDate={moment()}
maxDate={moment().add(5, "days")}
placeholderText="Select a date between today and 5 days in the future"
/>
But if you want to pick multiple non continous dates, you will have to refocus on the Date picker component in the onChange function so that the date picker will reappear and pick a date again.

how to diable previous dates on angularJS md datepicker

I am using Angular md-datepicker for startDate and endDate.
I need my startDate to equal currentDate and disable previous dates. Similarly user can choose endDate only after startDate.
In md-datepicker, you can set the minimum date using md-min-date. In your controller you can set a $scope variable like:
$scope.today = new Date()
and in view just use
<md-datepicker ng-model="startDate" md-placeholder="Enter date" md-min-date="today"></md-datepicker>
And for choosing endDate only after selecting startDate, you can make use of ng-disabled in the second date picker till your startDate model is set. So your second datepicker directive will look like
<md-datepicker ng-model="endDate" md-placeholder="Enter date" md-min-date="startDate" ng-disabled="startDate.length == 0"></md-datepicker>
Hope this helps.

How to only select dates in past in ng-bs-daterangepicker?

I need to make sure selected dates on ng-bs-daterangepicker are in the past.
Here is an example:
I have FROM and TO dates, I need to select dates to show in a graph.
With the first picker I need to set the "FROM" date, which has to be in the past and on the second picker I need select the "TO" date, this date should not be less than the FROM date, neither greater than today's date.
I tried this:
<input
type="daterange"
ng-model="dates"
format="L"
separator="/"
max-date={{todyDateRangeChart}};
opens="left"
/>
and in ctrl
$scope.todyDateRangeChart = new Date();
Here it is in Plunker
Fixed max-date.
They seem to worked only with string dates.
<input
type="daterange"
ng-model="dates"
format="L"
separator="/"
min-date="{{minDate}}"
max-date="{{maxDate}}"
opens="left" />
And in controller:
$scope.minDate = '2013-08-01';
$scope.maxDate = '2013-09-30';
Few additional changes to the controller: I also changed the order or scripts - angular is just a bit before ng-bootstrap-datepicker. And added the controller as ng-controller.
Plunker

Start Date and End Date in AngularJS

I am using bootstrap datepicker to for an input field "start date" . If the field is empty, another field "end date", which is also filled using a datepicker, should be disabled. Only after entering a date in the start date, the end date field should be enabled. I tried doing this using angular js. The sample code is below.
Start date
<input ng-model="startdate"/>
End date
<input ng-disabled="!(!!startdate)"
The end date remains disabled even after selecting a start date. Please help. If I try to get the value of the start date field it says "htmlinput element"
Please help
It's pretty easy!!
Try this out
Working Demo 1
Working Demo 2
Start Date :<input ng-model="startdate"/>
End Date :<input ng-disabled="!startdate"/>

Resources