How to compare dates in c#

后端 未结 3 1702
-上瘾入骨i
-上瘾入骨i 2020-12-02 22:01

I have two dates. One date is input and other is DateTime.Now. I have them in mm/dd/yyyy format, it can even be m/d/yy format also. Both dates are

3条回答
  •  旧时难觅i
    2020-12-02 22:37

    If you have your dates in DateTime variables, they don't have a format.

    You can use the Date property to return a DateTime value with the time portion set to midnight. So, if you have:

    DateTime dt1 = DateTime.Parse("07/12/2011");
    DateTime dt2 = DateTime.Now;
    
    if(dt1.Date > dt2.Date)
    {
         //It's a later date
    }
    else
    {
         //It's an earlier or equal date
    }
    

提交回复
热议问题