【问题标题】:How to only allow the current month in Month Calender如何在月历中只允许当前月份
【发布时间】:2015-01-11 20:21:52
【问题描述】:

我试图在我的 Windows 窗体中获取月历,只允许用户选择当月的日期,我的程序使用所选日期从文本文件中检索一行,但显然它会读取无论月份如何,第 1 天或第 20 天等的行。

如何设置属性,以便将月历锁定为当前月份??

类似以下内容

monthCalendar.MinDate = DateTime.CurrentMonth.DaysInMonth.First;
monthCalender.MaxDate = DateTime.CurrentMonth.DaysInMonth.Last;

【问题讨论】:

    标签: c# monthcalendar


    【解决方案1】:

    firstDayInMonth 和 lastDayInMonth 您可以使用此代码获得:

    DateTime today = DateTime.Today;
    DateTime firstDayInMonth = new DateTime(today.Year, today.Month, 1);
    DateTime lastDayInMonth = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));
    

    【讨论】:

    • 感谢完美运行的工厂。我很接近我首先尝试过 DateTime = new DateTime(today.Year, today.Month 但是当我到 Month 它要求 int year 所以我停下来说不,那一定是错的。谢谢
    猜你喜欢
    • 1970-01-01
    • 2014-01-25
    • 2015-01-30
    • 2021-12-03
    • 2019-11-09
    • 1970-01-01
    • 2015-12-11
    • 2013-05-05
    • 2021-12-27
    相关资源
    最近更新 更多