Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set MinDate and MaxDate in DatePIcker

Creating app in which i am showing DatePicker.Now i want to set MinDate of DatePicker is previous two years and max date future two years only.Selection should be base on current date.Suppose current Date is 23/11/2016 so datepicker should show date till 23/11/2014 in DatePicker all the date should be disabled before the 23/11/2014.And when we click on Datepicker cursor should be on current date.Created DtaePicker

private void showDateDailog() {

    final DatePickerDialog datePickerDialog = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDate) {

            year = selectedYear;
            month = selectedMonth;
            day = selectedDate;

          ((TextView) findViewById(R.id.textViewTORStartDate)).setText(new StringBuilder().append(day).append("/")
                        .append(month + 1).append("/").append(year));

        }
    }, year, month, day);
    datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
    datePickerDialog.show();
}
like image 990
user7108398 Avatar asked Nov 23 '16 08:11

user7108398


People also ask

How do you set your MinDate on datepicker?

To set it after init, use $('#datepicker'). datepicker( "option", "minDate", new Date( 1999, 10 - 1, 25 ) ) .

What is MinDate and maxDate in jQuery datepicker?

If you like to restrict access of users to select a date within a range then there is minDate and maxDate options are available in jQuery UI. Using this you can set the date range of the Datepicker. After defining these options the other days will be disabled which are not in a defined range.

How do I maxDate datepicker?

Try this: $("#datepicker"). datepicker({ minDate: -0, maxDate: new Date(2013, 1, 18) }); If you want use hard coded date, use the new Date(2013, 1, 18) pattern.

How can I set start and end dates for a datepicker?

If you want to set a fixed date rather than 'x days from now' then you can set this too: var start_date = new Date ('2013-06-21'); var end_date = new Date('2013-12-24'); Which will set the start date to 21 Juen 2103 and the end date to 24 Dec 2013.


Video Answer


3 Answers

To set the min date two years before and max two years after today use the following code:

Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -2); // subtract 2 years from now
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
c.add(Calendar.YEAR, 4); // add 4 years to min date to have 2 years after now
datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
like image 151
RustamG Avatar answered Oct 19 '22 15:10

RustamG


I believe this code really helps you to give the fix.

Here is the code for your fix -

private void showDateDailog() {

final DatePickerDialog datePickerDialog = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDate) {

        year = selectedYear;
        month = selectedMonth;
        day = selectedDate;

      ((TextView) findViewById(R.id.textViewTORStartDate)).setText(new StringBuilder().append(day).append("/")
                    .append(month + 1).append("/").append(year));

    }
}, year, month, day);

final Calendar calendar = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
//Min date setting part
cal.set(Calendar.MONTH, mm);
cal.set(Calendar.DAY_OF_MONTH, dd);
cal.set(Calendar.YEAR, yy - 2);
datePickerDialog.setMinDate(cal.getTimeInMillis());
//Maximum date setting part
Calendar calen = Calendar.getInstance();
calen.set(Calendar.MONTH, mm);
calen.set(Calendar.DAY_OF_MONTH, dd);
calen.set(Calendar.YEAR, yy + 2);
datePickerDialog.setMaxDate(calen.getTimeInMillis());
datePickerDialog.show();
}
like image 30
Bethan Avatar answered Oct 19 '22 14:10

Bethan


You can set MinDate and MaxDate for that you will need to use DatePicker class.

class MDatePickerDialog extends DatePickerDialog {
    MDatePickerDialog(Context c) {
        super(c, null, 2016, 11, 23);
        Date min = new Date(2018-1900, 4, 21);
        DatePicker p = getDatePicker();
        CalendarView cv = p.getCalendarView();
        long cur = cv.getDate();
        int d = cv.getFirstDayOfWeek();
        p.setMinDate(min.getTime());
        cv.setDate(cur + 1000L*60*60*24*40);
        cv.setFirstDayOfWeek((d + 1) % 7);
        cv.setDate(cur);
        cv.setFirstDayOfWeek(d);
    }
}

Hope this will help you.

like image 21
iSandeep Avatar answered Oct 19 '22 15:10

iSandeep