Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.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# mvc 2开始日期和结束日期验证_C#_Datetime_Asp.net Mvc 2 - Fatal编程技术网

C# mvc 2开始日期和结束日期验证

C# mvc 2开始日期和结束日期验证,c#,datetime,asp.net-mvc-2,C#,Datetime,Asp.net Mvc 2,大家好,我正在尝试比较两个日期,由于某种原因,如果我指定2012年5月25日(开始日期)和2012年5月31日(结束日期),下面的代码将返回false 这只发生在25号作为开始日期的情况下,如果我使用26号就可以了 public bool IsValidDate(DateTime startDate, DateTime endDate) { return startDate < endDate && endDate > startDate;

大家好,我正在尝试比较两个日期,由于某种原因,如果我指定2012年5月25日(开始日期)和2012年5月31日(结束日期),下面的代码将返回false

这只发生在25号作为开始日期的情况下,如果我使用26号就可以了

 public bool IsValidDate(DateTime startDate, DateTime endDate)
    {
        return startDate < endDate && endDate > startDate;
    }
public bool IsValidDate(日期时间开始日期、日期时间结束日期)
{
返回startDatestartDate;
}

有什么问题吗?

你一定是弄错了什么。对于指定的给定输入,此代码返回
true

class Program
{
    static void Main()
    {
        var startDate = new DateTime(2012, 5, 25);
        var endDate = new DateTime(2012, 5, 31);
        Console.WriteLine(IsValidDate(startDate, endDate));
    }

    public static bool IsValidDate(DateTime startDate, DateTime endDate)
    {
        return startDate < endDate && endDate > startDate;
    }
}

为什么要创建一个函数来检查
startDate

private void button1_Click(object sender, EventArgs e)
{
    DateTime startDate = new DateTime(2012 , 05 , 25);
    DateTime endDate = new DateTime(2012 , 05 , 31);

    bool rtnval = IsValidDate(startDate, endDate);

}


public bool IsValidDate(DateTime startDate, DateTime endDate)
{
    return startDate < endDate && endDate > startDate; 
}
private void按钮1\u单击(对象发送者,事件参数e)
{
DateTime startDate=新的日期时间(2012,05,25);
DateTime endDate=新的日期时间(2012,05,31);
bool rtnval=IsValidDate(开始日期、结束日期);
}
公共bool IsValidDate(日期时间开始日期、日期时间结束日期)
{
返回startDatestartDate;
}
此代码返回true

将其拆分,并检查是否具有所需的值

public bool IsValidDate(DateTime startDate, DateTime endDate)
{
    bool resulta = startDate < endDate; // break here
    bool resultb = endDate > startDate; // break here
    return startDate < endDate && endDate > startDate;
}
public bool IsValidDate(日期时间开始日期、日期时间结束日期)
{
bool resulta=startDatestartDate;//在此处中断
返回startDatestartDate;
}

//哎呀,我没有意识到它已经得到了回答

对于您指定的输入,代码不会返回false。请显示您的真实代码,包括调用此方法的代码。啊!我将日期指定为DateTime startDate=新日期时间(2012-05-25);编译器不使用coma。@RajuKumar-通过使用破折号,实际上将这些破折号解释为减法运算符,并从2012开始执行子track 5和25,然后将rsult传递给DateTime的单个int构造函数参数,该参数采用刻度数。这将是一个非常古老的约会。当然,2012减去5减去25比2012减去5减去30大,即使在日期数学中它会更大。
startDate
endDate>startDate
是同一条语句,您只需在这一点上添加额外的代码。
public bool IsValidDate(DateTime startDate, DateTime endDate)
{
    bool resulta = startDate < endDate; // break here
    bool resultb = endDate > startDate; // break here
    return startDate < endDate && endDate > startDate;
}