簡體   English   中英

如何在datetimepicker控件中限制用戶僅選擇當前月份日期?

[英]How to restrict user to select only current month dates in datetimepicker control?

我希望用戶僅選擇當前月份的日期,但我不知道應該在min和max date屬性中添加什么。

是否有“當前月份”之類的屬性?

由於MinDateMaxDate屬性都采用DateTime ,因此可以將它們設置為;

DateTimePicker1.MinDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

DateTimePicker1.MaxDate = (new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1))
                          .AddMonths(1).AddDays(-1);

是的,您可以使用屬性MinDate和MaxDate並設置可以在控件中選擇的最小和最大日期。

var currentYear = DateTime.Now.Year;
var currentMonth = DateTime.Now.Month;
int firstDayInCurrentMonth = 1;
int lastDayInCurrentMonth = DateTime.DaysInMonth(currentYear, currentMonth);

dateTimePicker.MinDate = new DateTime(currentYear, currentMonth, firstDayInCurrentMonth);
dateTimePicker.MaxDate = new DateTime(currentYear, currentMonth, lastDayInCurrentMonth);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM