2013-11-26 57 views
1

我在wpf c#中編寫Datepicker。它讓選定的一天總是本月結束,我需要我的應用程序檢查,如果一天是Saturday,所以它返回selectedday-2,如果它是Sundayday-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; 
    } 
} 
+0

而不是'日期時間d_temp =新的日期時間(System.DateTime.Now.Year,System.DateTime.Now.Month,System.DateTime.Now.Day);''使用DateTime d_temp = DateTime.Today;' – Enam

+0

月的最後一天,您可以使用'DateTime.DaysInMonth(year,month)'(但不使用您的'if-else'語句)。 – Enam

回答

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

Demonstration

+0

您的方法已經工作,丹克蒂姆 – user2054112

1

DayOfWeek方法返回DayOfWeek枚舉。

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

if (dayOfWeek == DayOfWeek.Sunday) {} 

if (dayOfWeek == DayOfWeek.Saturday) {} 

希望幫助

+0

請使用SO標記http://stackoverflow.com/editing-help – Enam

+0

啊是的抱歉。感謝您對此進行分類 –

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