c# - 使用 C# wpf 更改周末日期

标签 c# wpf datepicker weekend

我在 wpf c# 中编写了一个 Datepicker。它让选定的日期始终为月末,我需要我的应用程序检查该日期是否为 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;
    } 
}

最佳答案

这行不通:

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

因为DayOfWeek 是一个枚举,而"Sunday" 是一个字符串。你这样检查:

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

Demonstration

关于c# - 使用 C# wpf 更改周末日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20211861/

相关文章:

c# - 根据所选选项卡设置按钮的可见性

Swift KVO 绑定(bind)未更新

c# - 图片来源winforms

c# - 将 PropertyInfo.PropertyType 转换为枚举

c# - 将 InkCanvas 笔划转换为字节数组并再次转换回来

c# - 让方法对 DLL 中的 PropertyChanged 使用react

html - 使用 !important 覆盖 CSS 文件内联 CSS 不起作用

android - 如何在android中更改日期选择器对话框的默认年份(1900)?

c# - 为什么球没有在右边缘反弹

c# - 在线程等待WaitOne()时停止应用程序的正确方法