C# 确定特定月份的最大天数

C# 确定特定月份的最大天数,c#,asp.net,.net,C#,Asp.net,.net,从razor视图中,我使用jquery将所选月份和年份发送给控制器 在控制器内部,我接收这些选定的值并创建datetime属性 public ActionResult Index(int year, int month) { Date1 = new DateTime(year, month, 1); Date2 = new DateTime(year, month, 31); ... } 有些月有31天,有些月有30天,2月有28天(闰年内29天) 我的问题是: 如何识

从razor视图中,我使用jquery将所选月份和年份发送给控制器

在控制器内部,我接收这些选定的值并创建datetime属性

public ActionResult Index(int year, int month)
{
    Date1 = new DateTime(year, month, 1);
    Date2 = new DateTime(year, month, 31);
    ...
}
有些月有31天,有些月有30天,2月有28天(闰年内29天)

我的问题是:

如何识别这些月份,并根据
Date2
变量中设置的相应最大月日?

您可以在
DateTime
上使用:

Date2 = new DateTime(year, month, DateTime.DaysInMonth(year, month));

您需要
DateTime.DaysInMonth(年,月)