How to check if one DateTime is greater than the other in C#

前端 未结 10 864
遇见更好的自我
遇见更好的自我 2021-02-03 16:28

I have two DateTime objects: StartDate and EndDate. I want to make sure StartDate is before EndDate. How is this

10条回答
  •  不要未来只要你来
    2021-02-03 17:20

    I'd like to demonstrate that if you convert to .Date that you don't need to worry about hours/mins/seconds etc:

        [Test]
        public void ConvertToDateWillHaveTwoDatesEqual()
        {
            DateTime d1 = new DateTime(2008, 1, 1);
            DateTime d2 = new DateTime(2008, 1, 2);
            Assert.IsTrue(d1 < d2);
    
            DateTime d3 = new DateTime(2008, 1, 1,7,0,0);
            DateTime d4 = new DateTime(2008, 1, 1,10,0,0);
            Assert.IsTrue(d3 < d4);
            Assert.IsFalse(d3.Date < d4.Date);
        }
    

提交回复
热议问题