c# - 如何在 C#.net 中获取选定月份的最后日期?

标签 c# asp.net

我在 .aspx 页面中使用下拉列表选择月份。我必须在 .aspx.cs 页面中获取所选月份的最后日期。 (有的月份有30天,有的月份有31天)

我该怎么做?

最佳答案

无需自定义计算。

使用 System.DateTime.DaysInMonth(yearNum, monthNum)方法找出任​​何给定月份(也是最后一天)的天数。

就这么简单:

//Get days in month 2 (Feb) of year 2011. Returns 28.
int daysInFeb2011 = System.DateTime.DaysInMonth(2011, 2); 

MSDN 文档提供了更详尽的描述性示例:

        const int July = 7;
        const int Feb = 2;

        // daysInJuly gets 31.
        int daysInJuly = System.DateTime.DaysInMonth(2001, July);

        // daysInFeb gets 28 because the year 1998 was not a leap year.
        int daysInFeb = System.DateTime.DaysInMonth(1998, Feb);

        // daysInFebLeap gets 29 because the year 1996 was a leap year.
        int daysInFebLeap = System.DateTime.DaysInMonth(1996, Feb);

关于c# - 如何在 C#.net 中获取选定月份的最后日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4744660/

相关文章:

asp.net - 强制注销所有在线用户中的特定用户

c# - "Prefer 32-bit"编译器标志对 Visual Studio(C#、VB)意味着什么?

c# - 自定义异步 HTTP 处理程序仅在 IIS 7.5 集成模式下生成错误 500 - 为什么?

asp.net - .NET Parallel.ForEach 可能会弄乱 NHibernate 上下文 session

c# - 如何在 ASP.NET 中获取客户端日期和时间?

asp.net - 需要一个 Asp.net MVC 应用解决方案

asp.net - IIS ApplicationPoolIdentity没有对 'Temporary ASP.NET Files'的写权限

c# - 单元测试 - 最佳实践

c# - 我正在使用 linq 语句更新数据库中的数据

c# - Awesomium 在 WindowsBase.dll 中引发 ArgumentException