28

How do I get todays date one year ago in C#?

Jim Aho
  • 9,932
  • 15
  • 56
  • 87
JL.
  • 78,954
  • 126
  • 311
  • 459
  • 5
    Can you explain further what you are trying to achieve? Otherwise you'll get the answer DateTime.Now.AddYears(-1); – Lazarus Jul 28 '09 at 11:23

5 Answers5

75

Todays date one year ago would be

DateTime lastYear = DateTime.Today.AddYears(-1);
Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
  • 4
    FYI, This takes into account leap year: If the current instance represents the leap day in a leap year, the return value depends on the target date: If value + DateTime.Year is also a leap year, the return value represents the leap day in that year. For example, if four years is added to February 29, 2012, the date returned is February 29, 2016. If value + DateTime.Year is not a leap year, the return value represents the day before the leap day in that year. For example, if one year is added to February 29, 2012, the date returned is February 28, 2013. – CD Waddell Apr 24 '14 at 13:48
6

What do you mean by "last years date"?

If you just want the date of today minus one year, try the following:

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

I hope that is what you need.

UPDATE: Damn, I'm way to slow it seems :(

bbohac
  • 333
  • 2
  • 12
3
DateTime.Now.AddYears(-1);
CD..
  • 72,281
  • 25
  • 154
  • 163
  • They were literally seconds later than the other guy not intentionally repeating answers... this is kinda crazy... – Zargold May 24 '17 at 19:42
3

using Fluent DateTime http://fluentdatetime.codeplex.com/

var oneYearAgo = 1.Years().Ago();
Simon
  • 33,714
  • 21
  • 133
  • 202
2
DateTime.Now.AddYears(-1)
AdaTheDev
  • 142,592
  • 28
  • 206
  • 200