Disable weekends i.e Saturday and Sunday

How to disable Saturday and Sunday from my datepicker dialog. I am giving only mindate no max date is provided.

Tagged:

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    The Android control date picker dialog doesn't allow us to choose specific dates. Try to use Materialdatetimepicker: https://github.com/Plac3hold3r/PH.Wdullaer.Materialdatetimepicker
    You can restrict the selected days like:

    var dpd = new MaterialdDatePicker.DatePickerDialog();
    dpd.Initialize(
            this,
            now.Get(CalendarField.Year),
            now.Get(CalendarField.Month),
            now.Get(CalendarField.DayOfMonth)
    );
    
    // Setting Min Date to today date
    Calendar minDate = Calendar.Instance;
    dpd.MinDate = minDate;
    
    //// Setting Max Date to next 2 years
    Calendar maxDate = Calendar.Instance;
    maxDate.Set(CalendarField.Year, Calendar.Instance.Get(CalendarField.Year) + 2);
    dpd.MaxDate = maxDate;
    
    //Disable all SUNDAYS and SATURDAYS between Min and Max Dates
    for (Calendar loopdate = minDate; minDate.Before(maxDate); minDate.Add(CalendarField.Date, 1))
    {
        int dayOfWeek = loopdate.Get(CalendarField.DayOfWeek);
        if (dayOfWeek == Calendar.Sunday || dayOfWeek == Calendar.Saturday)
        {
            var disabledDays = new Calendar[1];
            disabledDays[0] = loopdate;
            dpd.SetDisabledDays(disabledDays);
        }
    }
    
Sign In or Register to comment.