English 中文(简体)
mvc 2 启动日期和结束日期验证
原标题:mvc 2 start date and end date validation

Hi guys I m 试图比较两个日期, 出于某种原因, 以下代码将返回假代码, 如果我指定 25/05/2012 (起始日期) 和 31/05/12 (结束日期) 。

只有在第25号被用作起始日期时才会发生这种情况,如果我使用第26号,则工作正常。

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

会有什么错?

最佳回答

您必须错误了某些东西。 对于给定的输入, 您指定了此代码返回 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;
    }
}

在控制台上打印 true

当然,现在重复两次完全相同的条件是毫无意义的。 指出一次条件就足够了:

public bool IsValidDate(DateTime startDate, DateTime endDate)
{
    return startDate < endDate;
}
问题回答

为什么你要设置一个函数来检查 startDate< endDate 是否已经启动?

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; 
}

这个代码是真实的!

拆分并检查您是否拥有您想要的值

public bool IsValidDate(DateTime startDate, DateTime endDate)
{
    bool resulta = startDate < endDate; // break here
    bool resultb = endDate > startDate; // break here
    return startDate < endDate && endDate > startDate;
}

/ O O O O Os I didn't realize it 被解答了 / OOs I didn't realize its answer





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...