Mahendra
Mahendra

Reputation: 271

Get one year back from current date

I want to get exactly one year back from the current date (which I am passing) in .Net. Can anyone provide me a function or code that will perform this?

Upvotes: 27

Views: 41112

Answers (5)

user8930129
user8930129

Reputation:

Try the following:

DateTime.AddYears(-1);

This worked for me.

Upvotes: 1

Arto Viitanen
Arto Viitanen

Reputation: 188

Like always, leap years make thing funny. If you have (I used embedded Boo interpreter on the SharpDevelop, thus the Boo syntax)

leap = DateTime(2004,2,29)
prev = leap.AddDays(-1)
Console.WriteLine(leap.AddYears(-1))
Console.WriteLine(prev.AddYears(-1))

you get the same day.

Upvotes: 0

Eedoh
Eedoh

Reputation: 6278

    public DateTime YearEarlier(DateTime mydate)
{
  return mydate.AddYears(-1);
}

Upvotes: 7

DevDemon
DevDemon

Reputation: 387

Try DateTime.AddYears(-1);

Upvotes: 13

tanascius
tanascius

Reputation: 53964

You can use the AddYears() method:

DateTime.Now.AddYears( -1 );

Upvotes: 60

Related Questions