Subtract date from a DateTime [duplicate]

2

I need to get current date and subtract 3 months.

It was doing conversions and subtracting 3 but going to go wrong at the turn of the year, in January for example it will result in month -2.

I think there is a better way to do this.

    
asked by anonymous 08.12.2017 / 13:52

1 answer

2

Yes, there is a more appropriate way. The DateTime structure has the method AddMonths , to subtract a number of numbers is to use this method with a negative parameter.

For example

var data = new DateTime(2017, 01, 01);
var novaData = data.AddMonths(-3);

See working in .NET Fiddle     

08.12.2017 / 13:55