How to compare dates in c#

后端 未结 3 1695
-上瘾入骨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条回答
  •  失恋的感觉
    2020-12-02 22:28

    If you have date in DateTime variable then its a DateTime object and doesn't contain any format. Formatted date are expressed as string when you call DateTime.ToString method and provide format in it.

    Lets say you have two DateTime variable, you can use the compare method for comparision,

    DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
    DateTime date2 = new DateTime(2009, 8, 2, 0, 0, 0);
    int result = DateTime.Compare(date1, date2);
    string relationship;
    
    if (result < 0)
       relationship = "is earlier than";
    else if (result == 0)
       relationship = "is the same time as";         
    else
       relationship = "is later than";
    

    Code snippet taken from msdn.

提交回复
热议问题