c# .net framework subtracting time-span from date

Posted by smkngspcmn on Stack Overflow See other posts from Stack Overflow or by smkngspcmn
Published on 2010-12-24T07:41:09Z Indexed on 2010/12/24 7:54 UTC
Read the original article Hit count: 153

Filed under:
|
|
|
|

I want to subtract a time-span from a date-time object. Date is 1983/5/1 13:0:0 (y/m/d-h:m:s) Time span is 2/4/28-2:51:0 (y/m/d-h:m:s)

I can use the native DateTime and TimeSpan objects to do this, after converting years and months of the time-span to days (assuming a 30 day month and a ~364 day year).

new DateTime(1981,5,1,13,0,0).Subtract(new TimeSpan(878,13,51,0));

With this i get the result:

{12/4/1978 11:09:00 PM}

But this is not exactly what i expected. If i do this manually (assuming a 30 day month) i get

1981/0/3-10:9:0

This is pretty close to what i'm after except i shouldn't get 0 for month and year should be 1980. So can someone please show me how i can do this manually and avoid getting a 0 month value? Also why do i get a completely different value when i use native classes?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET