How to check if a date has passed in C#?

前端 未结 6 1416
执笔经年
执笔经年 2021-01-05 03:24

I\'m reading the date expires cookie (2 hours) from database, and I need to check if this date has passed. What\'s the best way to do this?

For example:

<         


        
6条回答
  •  遥遥无期
    2021-01-05 04:02

    private enum DateComparisonResult
        {
            Earlier = -1,
            Later = 1,
            TheSame = 0
        };
    
        void comapre()
        {
            DateTime Date1 = new DateTime(2020,10,1);
            DateTime Date2 = new DateTime(2010,10,1);
    
            DateComparisonResult comparison;
            comparison = (DateComparisonResult)Date1.CompareTo(Date2);
            MessageBox.Show(comparison.ToString());    
        }
        //Output is "later", means date1 is later than date2 
    

    To check if date has passed:

    Source:https://msdn.microsoft.com/en-us/library/5ata5aya%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

提交回复
热议问题