开发者

NSDate 1 year ago

Consider a start date and an end date:

NSDate *startDate, *endDate;

For example, startDate could be Dec 10, 2010 and endDate could be Jan 4, 2011.

Consider the span of days from Dec 10, 2010 to Jan 4, 2011.

How could I determine the date range over that same span of days but exactly 1 year ago. And I'd like it to take into consideration leap years. So I'm unable to simply subtract 365 days from each date.

I'd like to end up with:

NSDate *oneYearAgoStartDate, *oneYearAgoEndDate.

So maybe in th开发者_开发问答is case, oneYearAgoStartDate would be Dec 10, 2009 and oneYearAgoEndDate would be Jan 4, 2010 (note that I haven't confirmed this).

How could I initialize these two variables to achieve what I need.


I'd say you want to take a look at NSCalendar, specifically the method -dateByAddingComponents:toDate:options:. In your instance, the NSDataComponents instance would have a year of -1.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜