Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用年数和月数计算一个月的天数?_C#_Asp.net_Asp.net Mvc 3_Datetime - Fatal编程技术网

C# 用年数和月数计算一个月的天数?

C# 用年数和月数计算一个月的天数?,c#,asp.net,asp.net-mvc-3,datetime,C#,Asp.net,Asp.net Mvc 3,Datetime,而不是使用: int noOfDaysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month); 我想使用传入的2个值来获取一个月的天数: public ActionResult Index(int? month, int? year) { DateTime Month = System.Convert.ToDateTime(month); DateTime Year = System.Conve

而不是使用:

int noOfDaysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
我想使用传入的2个值来获取一个月的天数:

public ActionResult Index(int? month, int? year)
{
    DateTime Month = System.Convert.ToDateTime(month);
    DateTime Year = System.Convert.ToDateTime(year);
    int noOfDaysInMonth = DateTime.DaysInMonth(Year, Month);

(年、月)是否标记为无效参数?有什么想法吗?可能是system.conert.todatetime.month?

DateTime.DaysInMonth方法没有重载,该方法使用两个
DateTime
实例。不要创建这两个
DateTime
实例,只需将收到的参数直接传递到
DaysInMonth


请注意,方法不能接受空值,因此要么删除空值,要么清理输入,即:检查年份和月份是否为空,如果为空,则使用一些默认值。

接受两个
DateTime
实例的
DateTime.DaysInMonth
方法没有重载。不要创建这两个
DateTime
实例,只需将收到的参数直接传递到
DaysInMonth


请注意,方法不能接受空值,因此要么删除空值,要么清理输入,即:检查年份和月份是否为空,如果为空,则使用一些默认值。

它们是
日期时间
变量,但需要
int
s:

int noOfDaysInMonth = DateTime.DaysInMonth(year.Value, month.Value);
如果它们可以为空:

int noOfDaysInMonth = -1;
if(year != null && month != null)
    noOfDaysInMonth = DateTime.DaysInMonth(year.Value, month.Value);

它们是
DateTime
变量,但需要
int
s:

int noOfDaysInMonth = DateTime.DaysInMonth(year.Value, month.Value);
如果它们可以为空:

int noOfDaysInMonth = -1;
if(year != null && month != null)
    noOfDaysInMonth = DateTime.DaysInMonth(year.Value, month.Value);

DateTime.DaysInMonth接受int参数,而不是日期时间参数

public static int DaysInMonth(
    int year,
    int month
)
不过要小心,您正在传递可为null的int。因此,请在检查它们是否有价值之前进行检查

if(month.HasValue && year.HasValue)
{
   var numOfDays = DaysInMonth(year.Value, month.Value);
}

DateTime.DaysInMonth接受int参数,而不是日期时间参数

public static int DaysInMonth(
    int year,
    int month
)
不过要小心,您正在传递可为null的int。因此,请在检查它们是否有价值之前进行检查

if(month.HasValue && year.HasValue)
{
   var numOfDays = DaysInMonth(year.Value, month.Value);
}

这里不需要使用任何
DateTime
对象,但需要验证输入

public ActionResult Index(int? month, int? year)
{
    int noOfDaysInMonth = -1;

    if(year.HasValue && year.Value > 0 && 
            month.HasValue && month.Value > 0 && month.Value <=12)
    {
        noOfDaysInMonth = DateTime.DaysInMonth(year.Value, month.Value);
    } 
    else
    {
        // parameters weren't there or they had wrong values
        // i.e. month = 15 or year = -5 ... nope!

        noOfDaysInMonth = -1; // not as redundant as it seems...
    }

    // rest of code.
}
公共行动结果索引(整数月、整数年)
{
int noOfDaysInMonth=-1;
如果(year.HasValue&&year.Value>0&&

month.HasValue&&month.Value>0&&month.Value此处不需要使用任何
DateTime
对象,但需要验证输入

public ActionResult Index(int? month, int? year)
{
    int noOfDaysInMonth = -1;

    if(year.HasValue && year.Value > 0 && 
            month.HasValue && month.Value > 0 && month.Value <=12)
    {
        noOfDaysInMonth = DateTime.DaysInMonth(year.Value, month.Value);
    } 
    else
    {
        // parameters weren't there or they had wrong values
        // i.e. month = 15 or year = -5 ... nope!

        noOfDaysInMonth = -1; // not as redundant as it seems...
    }

    // rest of code.
}
公共行动结果索引(整数月、整数年)
{
int noOfDaysInMonth=-1;
如果(year.HasValue&&year.Value>0&&

month.HasValue&&month.Value>0&&month.Value如果为空怎么办?如果为空怎么办?