iphone:找到第4天的日期

时间:2011-06-21 12:16:39

标签: iphone cocoa-touch date

我使用NSDateComponents查找一周中的第4天,第2天的日期

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit |
                                                                 NSMonthCalendarUnit |
                                                                 NSYearCalendarUnit |
                                                                 NSWeekCalendarUnit | 
                                                                 NSWeekdayCalendarUnit)
                                                       fromDate:today];
[weekdayComponents setWeekday:4];


NSLog(@"Date at that day was %@",[gregorian dateFromComponents:weekdayComponents]);

但每次输出

  

当天的日期是2011-06-20 18:30:00 +0000

你能帮忙吗? 提前致谢

1 个答案:

答案 0 :(得分:2)

[weekdayComponents setDay:weekdayComponents.day+ (4-weekdayComponents.weekday)];

至于NSlog输出:NSLog(@“%@”,somedate)以GMT0显示时间,但NSDateComponents的动作考虑了时区,所以2011-06-20 18:30:00 +0000是在您的时区开始于2011年6月21日