Subtract date from datetime

Asked

Viewed 4,309 times

2

I need to take current date and subtract 3 months.

Bang making conversions and subtracting 3 but will go wrong to turn the year, in January for example will result in month -2.

I believe that there is a better way of doing this.

  • Now it worked out, I’m familiar with the platform. Thanks for the tips.

1 answer

2


Yes, there is a more suitable way. The structure DateTime has the method AddMonths, to subtract a number amount 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 on . NET Fiddle

  • It worked perfectly, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.