Bootstrap datepicker disable dates for certain fields - asp.net-core

I have series of bootstrap date fields like this
<input id="TxtNextReviewDate" type="text" asp-for="RequestFormMaster.NextReviewDate" class="form-control form-control-sm datepicker">
<input id="DateSalesRequest" asp-for="SLAInformation.SalesDateOfSubmission" type="text" class="form-control form-control-sm datepicker" />
<input id="DateFinalInfoReceived" asp-for="SLAInformation.SalesFinalInfoReceived" type="text" class="form-control form-control-sm datepicker" />
i want to disable past dates for Next Review Date Calendar(First Calendar input).For the rest i should able to select the past dates.
This is how i am disabling the past dates, its disabling other date field also.how can i restrict this for only one field?
<script>
var date = new Date();
date.setDate(date.getDate());
$('.datepicker').datepicker({
format: 'dd/mm/yyyy',
startDate: date
});
</script>
Thanks,
Teena

how can i restrict this for only one field?
Three Datepicker inputs contains different id. You can use id selector to distinguish the datepicker.
startDate: date cannnot disable the past date selection, it just set the Datepicker's start date. You need use minDate: 0 to disable past date selection.
Change your code like below:
$('#DateSalesRequest,#DateFinalInfoReceived').datepicker({
format: 'dd/mm/yyyy'
});
$('#TxtNextReviewDate').datepicker({
format: 'dd/mm/yyyy',
minDate: 0
});
Result:
minDate is not working for me. However i managed to fix the issue. Thanks for the hint.
<script>
var date = new Date();
date.setDate(date.getDate());
$('#TxtNextReviewDate').datepicker({
format: 'dd/mm/yyyy',
startDate: date
});
$('#TxtLastDate,#TxtDateFinalApproval').datepicker({
format: 'dd/mm/yyyy',
});
</script>

Related

Element UI - DateTimePicker how to disable past dates vue.js

I am using element ui datepicker from there. I want to disable past dates.
<el-date-picker
v-model="form.startDate"
type="date"
placeholder="Tarih Seçiniz."
style="width: 100%"
/>
Can you help me to handle this issue?
You can achieve this by adding a :disabled-date attribute in the <el-date-picker> element and pass the boolean value to this attribute based on the calculation.
<el-date-picker :disabled-date="disabledDate" ...
In Script:
const disabledDate = (time: Date) => {
const date = new Date();
const previousDate = date.setDate(date.getDate() - 1);
return time.getTime() < previousDate;
}

Vue Formulate Date Picker Validation

I've been using the Vue Formulate library (which is awesome).
I need the user to only be able to pick a date from today (included) onwards.
I am using the "after" default for validation, but today (the current date) is not valid. In other words the validation kicks in when choosing todays date, and the form cannot be submitted.
What is the best way to get around this?
https://codesandbox.io/s/vue-formulate-reproduction-template-forked-kmpgq?fontsize=14&hidenavigation=1&theme=dark
A little workaround by giving the after attribute a Date value before today:
<template>
<FormulateInput
name="date"
label="Date:"
:validation="
'bail|required|after:' + new Date(Date.now() - 24 * 60 * 60 * 1000)
"
:validation-messages="{ after: 'The date has to be today or later' }"
type="date"
/>
</template>
https://codesandbox.io/s/vue-formulate-reproduction-template-forked-ky1hu?fontsize=14&hidenavigation=1&theme=dark
EDIT: In fact you can do it also with a computed property by returning the date before today.
After validation should defined by a Date. Here is the solution by calculating today by a computed property. Codesandbox
<template>
<FormulateInput
name="date"
label="Date:"
:validation="'bail|required|after:' + today"
:validation-messages="{ after: 'The date has to be today or later' }"
type="date"
/>
</template>
<script>
export default {
computed: {
today() {
return new Date().toLocaleDateString();
},
},
};
</script>

How to display custom validation for date picker in ngx-formly/bootstrap

I am creating dynamic UI/form using ngx-formly/bootstrap(not using material). I want to display datepicker control so I have displyed custom bsdatepicker control using ngx-bootstrap/datepicker. Now I want to do validations on this datepicker e.g IfI have from and to date then From date should not be less than current date or To Date should not be grater than from date. Any help will be appriciated.
//datepicker.html
<pre><input type="text"
id="dob-id"
class="form-control calendar"
placement="bottom"
bsDatepicker
[formlyAttributes]="field"
#dobDate="bsDatepicker"
[bsConfig]="bsConfig"
placeholder="YYYY-MM-DD"
[class.is-invalid]="showError" class="" style="width: 350px;">
took separate component for this and registered in app.modeul
//in scheema
{
key: 'date1',
type: 'bsdatepicker',
templateOptions: {
label :'From Date',
required: true,
},
},
{
key: 'date2',
type: 'bsdatepicker',
templateOptions: {
label :'To Date',
required: true,
},
},
The validation is handled by Formly so all you need is to just define validation along with its messages you may check our examples in the docs:
https://formly.dev/examples/validation/built-in-validations
https://formly.dev/examples/validation/custom-validation
https://formly.dev/examples/validation/validation-message
ngx-bootstrap is a wrong tag here, I think

How to set months to display in bsDaterangepicker popup?

I am using bsDaterangepicker when clicked it shows current month and next in the popup. All date ranges in my application are in the past, so I need to show current and previous month. How can I configure that?
You use this workaround to solve it:
In HTML:
<input
formControlName="dateRange"
type="text"
bsDaterangepicker
#rangePicker="bsDaterangepicker"
(onShown)="onDateRangePickerShow()"/>
</div>
In component:
export class Component {
#ViewChild('rangePicker') rangePicker;
onDateRangePickerShow() {
// This is a workaround to show previous month
const prevMonth = new Date(moment().subtract(1, 'month'));
this.rangePicker._datepicker.instance.monthSelectHandler({ date: prevMonth });
}

How I can use bootstrap datepicker with year and month drop down option, as well as with the min date and max date

I need a bootstrap datepicker where the start date and enddate must be customizable like start date is 01-01-1950 and end date is todate-18 years. Secondly wanted to show the dropdown year and month on the calendar topbar to use easily rather than the default bootstrap calendar.
For your reference have attached an image which 'm using now without bootstrap.
Any idea/link is appreciated.
you can not put dropdown but can achieve same functionality by another way with new bootstrap version.
check out this link http://www.malot.fr/bootstrap-datetimepicker/
<script>
$(document).ready(function() {
$('#date_deb').daterangepicker({locale: {format: 'DD/MM/YYYY'},
showDropdowns: true,
minDate: '01/01/2016',
singleDatePicker: true,
singleClasses: "picker_2"
}, function(start, end, label) {
console.log(start.toISOString(), end.toISOString(), label);
});
});
</script>