0
我正在尝试做一些简单的7天历史绘图。鉴于目前的NSDate.date
,我想获得对应于7天前的开始的NSDate。所以基本上是在今天早上0.00前的7天。从7天前开始获取NSDate
我已经试过,如下:
// decompose the current date, do I need more component fields?
NSDateComponents *comps = [NSCalendar.currentCalendar components: NSDayCalendarUnit fromDate: NSDate.date];
NSLog(@"components: %@", comps);
// Back day up 7 days. Will this wrap appropriate across month/year boundaries?
comps.day -= 7;
NSDate *origin = comps.date;
NSLog(@"new date: %@", origin);
我认为是由刚刚指定NSDayCalendarUnit
,其他的事情会违约(如一天开始,等)。不幸的是,origin
结束为(null)
。什么是正确的方法来做到这一点?
我实际上希望日期是相对于当前时区。 IOW,如果我在PST中,我希望7天前在PST中开始一天。那个'.timeZone'制定者会帮助还是伤害那个? – 2015-02-24 16:32:15
通过反复试验回答了我自己的问题。的确,我不想要'.timeZone'集。但'.calendar'是必需的。奇怪的是,你认为返回组件的日历会设置它的日历,因为它是从它派生出来的。 – 2015-02-24 16:40:46