Question

I write a Datepicker in wpf c#. Its let the selected day always of the end of the Month and I need that my Application check if the day is Saturday so its return the selectedday-2 and if it is Sunday than day-1 but this weekend function it doesn't work. I didn't see where is the Error

Code:

public partial class MainWindow : Window
{
    public void weekend(DatePicker dp1, DateTime d_temp)
    {
       if (d_temp.DayOfWeek.Equals("Sunday"))
        {
            dp1.SelectedDate = new DateTime(d_temp.Year, d_temp.Month, d_temp.Day-2);
        }
       if (d_temp.DayOfWeek.Equals("Saturday"))
        {
            dp1.SelectedDate = new DateTime(d_temp.Year, d_temp.Month, d_temp.Day-1);
       }
    }

    public MainWindow()
    {
        InitializeComponent();
        DateTime d_temp = new DateTime(System.DateTime.Now.Year, System.DateTime.Now.Month, System.DateTime.Now.Day);

        if (d_temp.Month == 2 )
        {
            dp1.SelectedDate = new DateTime(d_temp.Year, d_temp.Month, 28);
        }
        if (d_temp.Month >= 1 && d_temp.Month <= 7)
        {
            if (d_temp.Month % 2 == 0)
            {
                dp1.SelectedDate = new DateTime(d_temp.Year, d_temp.Month, 30);



                    weekend(dp1, d_temp);


            }
            else
            {
                dp1.SelectedDate = new DateTime(d_temp.Year, d_temp.Month, 31);
                weekend(dp1, d_temp);
            }
        }
        if (d_temp.Month > 7)
        {
            if (d_temp.Month % 2 == 0)
            {
                dp1.SelectedDate = new DateTime(d_temp.Year, d_temp.Month, 31);
                weekend(dp1, d_temp);
            }
            else
            {
                dp1.SelectedDate = new DateTime(d_temp.Year, d_temp.Month, 30);
               weekend(dp1, d_temp);
            }
        }       
    }


    private void Button_Click(object sender, RoutedEventArgs e)
    {
        dp1.SelectedDate = System.DateTime.Now;
    } 
}
Was it helpful?

Solution

This doesn't work:

if (d_temp.DayOfWeek.Equals("Sunday"))

because DayOfWeek is an enum whereas "Sunday" is a string. You check it in this way:

if(d_temp.DayOfWeek == DayOfWeek.Sunday)

or (worse):

if(d_temp.DayOfWeek.ToString() == "Sunday")

You could use this method:

public static DateTime GetEndOfMonth(DateTime start, bool workingDaysOnly)
{
    int year = start.Year;
    int month = start.Month;
    int daysInMonth = CultureInfo.CurrentCulture.DateTimeFormat.Calendar.GetDaysInMonth(year, month);
    var dt = new DateTime(year, month, daysInMonth);
    if (workingDaysOnly)
    {
        switch (dt.DayOfWeek)
        {
            case DayOfWeek.Saturday:
                dt = dt.AddDays(-1);
                break;
            case DayOfWeek.Sunday:
                dt = dt.AddDays(-2);
                break;
            default:
                break;
        }
    }
    return dt;
}

Usage:

DateTime endOfMonth = GetEndOfMonth(DateTime.Today, true);

Demonstration

OTHER TIPS

The DayOfWeek method returns the DayOfWeek enum.

If you just change the .equals to == DayOfWeek.day

if (dayOfWeek == DayOfWeek.Sunday) {}

if (dayOfWeek == DayOfWeek.Saturday) {}

Hope that helps

I changed your weekend() function (now it is setBusinessDay()). Also i modified your MainWindow() code:

    public DateTime setBusinessDay(DateTime dt)
    {
        if (dt.DayOfWeek == DayOfWeek.Saturday)
            return dt.AddDays(-1);
        else if (dt.DayOfWeek == DayOfWeek.Sunday)
            return dt.AddDays(-2);
        else
            return dt;
    }

    public MainWindow()
    {
        InitializeComponent();
        DateTime d_temp = DateTime.Today;

        dp1.SelectedDate = setBusinessDay(new DateTime(d_temp.Year, d_temp.Month, DateTime.DaysInMonth(d_temp.Year, d_temp.Month)));
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top