How to disable saturday and sunday from datepickerdialog

How to disable saturday and sunday from datepickerdialog in xamarin android. Here I provided only mindate but not max date. and I am getting the business days by using the following code.
public double getbusinessdays(DateTime stdate, DateTime eddate)
{
double businessdays = 1 + ((eddate - stdate).TotalDays * 5 - (stdate.DayOfWeek - eddate.DayOfWeek) * 2) / 7;
if(eddate.DayOfWeek == DayOfWeek.Saturday) {
businessdays--;
}
if(stdate.DayOfWeek == DayOfWeek.Sunday)
{
businessdays--;
}
return businessdays;
}

But Now I want to disable the saturday and sunday.

Tagged:

Answers

  • Moses_SundayMoses_Sunday Member ✭✭

    You can do it by using given code.

    if (dateTimePicker1.Text.Contains("Saturday")) { dateTimePicker1.Text = dateTimePicker1.Value.AddDays(2).ToString(); } else if(dateTimePicker1.Text.Contains("Sunday")) { dateTimePicker1.Text = dateTimePicker1.Value.AddDays(1).ToString(); }

    How to stop a user by selecting saturday and sunday.
    simple by Passing Message to user

    if (dateTimePicker1.Text.Contains("Saturday") || dateTimePicker1.Text.Contains("Sunday")) { // Pass Message Here }

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited October 11

    Traditional datepicker Calender doesn't support to disable all Sundays and Saturdays in calendar datepicker dialog so that nobody will be able to select disabled dates. Material Datepicker is a good choice to disable specific dates in Datepicker. It provides very useful methods to take Calander array of days like:

    setSelectableDays(Calendar[] days): The values in this list are the only acceptable dates for the picker.
    setDisabledDays(Calendar[] days): The values in this Calendar[] are explicitly disabled (not selectable).

    Sample code:

    DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(MainActivity.this, Year, Month, Day);
    datePickerDialog.setThemeDark(false);
    datePickerDialog.showYearPickerFirst(false);
    datePickerDialog.setTitle("Date Picker");
    
    
    // Setting Min Date
    Calendar min_date_c = Calendar.getInstance();
    datePickerDialog.setMinDate(min_date_c);
    
    
    
    // Setting Max Date
    Calendar max_date_c = Calendar.getInstance();
    max_date_c.set(Calendar.YEAR, Year + 2);
    datePickerDialog.setMaxDate(max_date_c);
    
    //Disable all SUNDAYS and SATURDAYS between Min and Max Dates
    for (Calendar loopdate = min_date_c; min_date_c.before(max_date_c); min_date_c.add(Calendar.DATE, 1), loopdate = min_date_c)
    {
        int dayOfWeek = loopdate.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == Calendar.SUNDAY || dayOfWeek == Calendar.SATURDAY)
        {
            Calendar[] disabledDays = new Calendar[1];
            disabledDays[0] = loopdate;
            datePickerDialog.setDisabledDays(disabledDays);
        }
    }
    

    For more details, check the Tutorials:
    https://www.freakyjolly.com/android-material-datepicker-and-timepicker-by-wdullaer-tutorial-by-example/#more-2649

    https://github.com/wdullaer/MaterialDateTimePicker

Sign In or Register to comment.