今天上午写了一段代码,是使用当前时间减去一些天作为查询条件,可是一调试,怎么弄都少一天,后来突然想起可能是精度问题,四舍五入给弄掉了。

看看下面的代码,就差了一条语句的执行时间,结果就出了一天的偏差。写出来的代码,想当然是不行的,还得测呀!
最初我是这么写,以为没有问题,结果一运行就是少一天
1DateTime.Now的精度这么高! (转)            DateTime a = DateTime.Now;
2DateTime.Now的精度这么高! (转)            DateTime b = DateTime.Now.AddDays(-14);
3DateTime.Now的精度这么高! (转)            TimeSpan c = a.Subtract(b);
4DateTime.Now的精度这么高! (转)            int d = c.Days;// d = 13

后来调了一下顺序就正常了

1DateTime.Now的精度这么高! (转)            DateTime b = DateTime.Now.AddDays(-14);
2DateTime.Now的精度这么高! (转)            DateTime a = DateTime.Now;
3DateTime.Now的精度这么高! (转)            TimeSpan c = a.Subtract(b);
4DateTime.Now的精度这么高! (转)            int d = c.Days;// d = 14

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2021-07-18
  • 2022-12-23
  • 2021-11-30
猜你喜欢
  • 2021-08-01
  • 2022-12-23
  • 2021-11-23
  • 2022-01-01
  • 2021-12-17
  • 2022-12-23
相关资源
相似解决方案