Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

A gotcha to look out for in .NET's implementation is that because the library is adjusting invalid results down to the last day of the month, addition and subtraction operations are not commutative:

  new DateTime(2012, 3, 31)
    .AddMonths(-1)
    .AddMonths(1) ==> 2012-03-29

  new DateTime(2012, 3, 31)
    .AddMonths(1)
    .AddMonths(-1) ==> 2012-03-30


Right, which is why an understanding of how .NET handles the calculations is important. If it's important that you preserve things like "last day of the month" there are libraries[0] which support this and will do date calculations accordingly.

[0] For example: http://sourceforge.net/projects/dday-ical/




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: