DateTime Comparison Precision

后端 未结 10 642
攒了一身酷
攒了一身酷 2020-12-29 04:24

I\'m doing DateTime comparison but I don\'t want to do comparison at second, millisecond and ticks level. What\'s the most elegant way?

If I simply compare the DateT

10条回答
  •  没有蜡笔的小新
    2020-12-29 04:48

    One approach could be to create two new DateTimes from your values you want to compare, but ignore anything from the seconds on down and then compare those:

    DateTime compare1 = new DateTime(year1, month1, day1, hour1, minute1, 0);
    DateTime compare2 = new DateTime(year2, month2, day2, hour2, minute2, 0);
    
    int result = DateTime.Compare(compare1, compare2);
    

    I'd be the first to admit it's not elegant, but it solves the problem.

提交回复
热议问题