我在wpf c#中編寫Datepicker
。它讓選定的一天總是本月結束,我需要我的應用程序檢查,如果一天是Saturday
,所以它返回selectedday-2
,如果它是Sunday
比day-1
但本週末功能它不起作用。我沒看到哪裏是錯誤更改日期如果它的週末與C#wpf
代碼:
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;
}
}
而不是'日期時間d_temp =新的日期時間(System.DateTime.Now.Year,System.DateTime.Now.Month,System.DateTime.Now.Day);''使用DateTime d_temp = DateTime.Today;' – Enam
月的最後一天,您可以使用'DateTime.DaysInMonth(year,month)'(但不使用您的'if-else'語句)。 – Enam