2016-02-11 83 views
-1

以下代碼給出上個月的最後一天。MonthCalendar1選定年份的第一天

MonthCalendar1.SelectionEnd = MonthCalendar1.SelectionStart.AddDays(-MonthCalendar1.SelectionStart.Day) 

哪個代碼給出MonthCalendar1選定年份的第一天?

因此,如果選擇日期是例如13.06.2015如何獲得01.01.2015?

回答

0

如果你想要去與你目前的新增排除的方法天,簡單地從DateTime.DayOfYear減去一個:

MonthCalendar1.SelectionEnd = MonthCalendar1.SelectionStart.AddDays(-(MonthCalendar1.SelectionStart.DayOfYear-1)) 

如果你想選擇一年的最後一天,使用DateTime.DaysInMonth()

Dim dt As New DateTime(MonthCalendar1.SelectionStart.Year, 12, DateTime.DaysInMonth(MonthCalendar1.SelectionStart.Year, 12)) 
+0

謝謝Idle_Mind。解決了。類似的問題:如果選擇日期是例如13.06.2015如何獲得31.12.2015?所以這次我的問題是關於MonthCalendar1選定年份的最後一天。請注意,+365不起作用,因爲有些年份包含366天:-)'MonthCalendar1.SelectionStart.AddDays( - (MonthCalendar1.SelectionStart.DayOfYear - 1)+ 365)' –

+0

請參閱編輯回答您的其他問題... –

2

使用基準日類似這樣的僅一年只需創建一個新的日期:

Dim firstOfYearDate As Date = New Date(baseDate.Year, 1, 1)

喜歡這個

MonthCalendar1.MaxSelectionCount = 365 
MonthCalendar1.SelectionEnd = New Date(2015, 06, 13) 
MonthCalendar1.SelectionStart = New Date(MonthCalendar1.SelectionEnd.Year, 1, 1) 
+0

是選定結束和或SelectionStart是日期字段?如果是這樣,他們不能設置爲新的日期值。 –

+0

我添加了特別使用MonthCalendar控件的代碼 –

相關問題