我在表单中有一个DateTimePicker
。我需要限制用户仅从当前月份中选择一个日期:例如,从1 November 2018
到30 November 2018
。
我尝试了以下代码行:
DtpIssue.MinDate = New Date Time(DateTime.Now.Year, DateTime.Now.Month, 1)
DtpIssue.MaxDate = New Date Time(DateTime.Now.Year, DateTime.Now.Month, 1)
但是它仅显示当前日期:10 November 2018
。
我该怎么做才能确定DateTimePicker
的当前月份?
答案 0 :(得分:1)
DateTime.DaysInMonth()返回月份的最后一天,并传递当前的年和月。
Dim FirstOfMonth As Date = New DateTime(Date.Now.Year, Date.Now.Month, 1)
Dim EndOfMonth As Date =
New DateTime(Date.Now.Year, Date.Now.Month, Date.DaysInMonth(Date.Now.Year, Date.Now.Month))
以类似的格式应用于您的控件:
DtpIssue.MinDate = New Date(Today.Year, Today.Month, 1)
DtpIssue.MaxDate = New Date(Today.Year, Today.Month, Date.DaysInMonth(Today.Year, Today.Month))