1

Datepicker我在 wpf c# 中写了一个。它让选定的日期总是在月底,我需要我的应用程序检查这一天是否Saturday如此它返回selectedday-2,如果是这样Sundayday-1但这个周末功能它不起作用。我没有看到错误在哪里

代码:

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;
    } 
}
4

3 回答 3

3

这不起作用:

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

因为DayOfWeek是一个枚举,"Sunday"而是一个string. 您以这种方式检查它:

if(d_temp.DayOfWeek == DayOfWeek.Sunday)

或(更糟):

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

你可以使用这个方法:

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;
}

用法:

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

示范

于 2013-11-26T08:33:25.317 回答
1

DayOfWeek方法返回DayOfWeek枚举。

如果你只是.equals改变== DayOfWeek.day

if (dayOfWeek == DayOfWeek.Sunday) {}

if (dayOfWeek == DayOfWeek.Saturday) {}

希望有帮助

于 2013-11-26T08:40:50.927 回答
0

我改变了你的weekend()功能(现在是setBusinessDay())。我还修改了您的MainWindow()代码:

    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)));
    }
于 2013-11-26T08:52:51.283 回答