DateTime Comparison Precision

后端 未结 10 633
攒了一身酷
攒了一身酷 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条回答
  •  猫巷女王i
    2020-12-29 04:50

    Another way is to convert first by processing on ticks level with a simple (non-rounding) calculation:

    var now = DateTime.UtcNow;
    // 636340541021531973, 2017-06-26T06:08:22.1531973Z
    
    var millisecondsPrecision = new DateTime(now.Ticks / 10000 * 10000, now.Kind);
    // 636340541021530000, 2017-06-26T06:08:22.1530000Z
    
    var secondsPrecision = new DateTime(now.Ticks / 10000000 * 10000000, now.Kind);
    // 636340541020000000, 2017-06-26T06:08:22.0000000Z
    
    var minutePrecision = new DateTime(now.Ticks / (10000000*60) * (10000000*60), now.Kind);
    // 636340541000000000, 2017-06-26T06:08:00.0000000Z
    

提交回复
热议问题