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);
        }
    }
    
  • LijuDanielLijuDaniel Member ✭✭

    It's Possible with another control :smile: "XamForms.Controls.Calendar"
    github.com/rebeccaXam/XamForms.Controls.Calendar/wiki

    Here you can add special dates in this control; if it is SAT or SUN then set your special dates and its property Selectable = false

    for (DateTime i = MinimumDate; i < MinimumDate.AddMonths(2); i = i.AddDays(1))
    {
    if ((i.DayOfWeek == DayOfWeek.Saturday) || (i.DayOfWeek == DayOfWeek.Sunday))
    {
    SpecDate.Add(new SpecialDate(i)
    {
    TextColor = Color.Gray,
    Selectable = false,
    FontSize = 10,
    BackgroundPattern = new BackgroundPattern(1)
    {
    Pattern = new List
    {
    new Pattern{ WidthPercent = 1f, HightPercent = 1f, Color = Color.Transparent,Text = "", TextSize =11, TextAlign=TextAlign.CenterBottom}
    }
    }
    });
    }
    }
    
Sign In or Register to comment.