开发者

Get one year back from current date

I want to get exactly one year back from the current date (w开发者_StackOverflow中文版hich I am passing) in .Net. Can anyone provide me a function or code that will perform this?


You can use the AddYears() method:

DateTime.Now.AddYears( -1 );


Try DateTime.AddYears(-1);


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


Try the following:

DateTime.AddYears(-1);

This worked for me.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜