Noda Time: Period.Between returning incorrect value

ぐ巨炮叔叔 提交于 2019-12-05 16:30:28

Richard was right in his comment on the OP. The problem is that the months and years aren't distinct quantities unto themselves. One must have a frame of reference to "count" them. You have that reference when you do the Period.Between operation, but it's lost by the time you try to add the periods together.

If you check the periods that are being added, it makes sense:

First:  Years: 3, Months: 11, Days: 30
Second: Years: 6, Months: 0,  Days: 22
Total:  Years: 9, Months: 11, Days: 52

In order to round as you would like, the 22 days being added to the 30 days would somehow have to know which month was being referenced. Even if you retained the original information - which one would you use? You could well have a 28-day month on one side, and a 31-day month on the other.

The best you could do would be to artificially round the results yourself afterwards, and choose a flat value (such as 30 days) to represent all months.

Oh, one minor thing (unrelated to your question) - To go from a DateTime to a LocalDate, try LocalDateTime.FromDateTime(dt).Date. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!