Home » c# » How to subtract a year from the datetime?

How to subtract a year from the datetime?

Posted by: admin November 29, 2017 Leave a comment


How to subtract a year from current datetime using c#?

var myDate = DateTime.Now;
var newDate = myDate.AddYears(-1);

DateTime oneYearAgoToday = DateTime.Now.AddYears(-1);

Subtracting a week:

DateTime weekago = DateTime.Now.AddDays(-7);


It might be worth noting that the accepted answer may adjust the date by either 365 days or 366 days due to leap years (it gets the date for the same day of the month one year ago, with the exception of 29th February where it returns 28th February).

In the vast majority of cases this is exactly what you want however if you are treating a year as a fixed unit of time (e.g. the Julian year) then you would need to subtract from either days;

        var oneFullJulianYearAgo = DateTime.Now.AddDays(-365.25);

or seconds;

        var oneFullJulianYearAgo = DateTime.Now.AddSeconds(-31557600);