DateTimePicker仅适用于当前月份

时间:2018-11-10 08:16:52

标签: vb.net winforms datetime datetimepicker monthcalendar

我在表单中有一个DateTimePicker。我需要限制用户仅从当前月份中选择一个日期:例如,从1 November 201830 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的当前月份?

1 个答案:

答案 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))