在IOS SDK中获取几周的date

我想用当天的名字得到当前和下周的date。

即如果今天的date是28-06-2013(星期五),那么我想从本周23-06-2013(星期日)到29-06-2013(星期六)

下周我想要得到date从30-06-2013(星期日)到06-07-2013(星期六)。

我怎样才能做到这一点?

谢谢,

这里是示例代码,伎俩。

-(NSArray*)daysThisWeek { return [self daysInWeek:0 fromDate:[NSDate date]]; } -(NSArray*)daysNextWeek { return [self daysInWeek:1 fromDate:[NSDate date]]; } -(NSArray*)daysInWeek:(int)weekOffset fromDate:(NSDate*)date { NSCalendar *calendar = [NSCalendar currentCalendar]; //ask for current week NSDateComponents *comps = [[NSDateComponents alloc] init]; comps=[calendar components:NSWeekCalendarUnit|NSYearCalendarUnit fromDate:date]; //create date on week start NSDate* weekstart=[calendar dateFromComponents:comps]; if (weekOffset>0) { NSDateComponents* moveWeeks=[[NSDateComponents alloc] init]; moveWeeks.week=1; weekstart=[calendar dateByAddingComponents:moveWeeks toDate:weekstart options:0]; } //add 7 days NSMutableArray* week=[NSMutableArray arrayWithCapacity:7]; for (int i=1; i<=7; i++) { NSDateComponents *compsToAdd = [[NSDateComponents alloc] init]; compsToAdd.day=i; NSDate *nextDate = [calendar dateByAddingComponents:compsToAdd toDate:weekstart options:0]; [week addObject:nextDate]; } return [NSArray arrayWithArray:week]; } 

我要复制并粘贴一个答案,但我会build议去阅读这篇文章: 本周开始和结束date 。 它看起来会帮助你达到你想要的。

也许这样的东西:

 NSDate *now = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSInteger weekNumber = [[calendar components: NSWeekCalendarUnit fromDate:now] week]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *comp = [gregorian components:NSYearCalendarUnit fromDate:now]; [comp setWeek:weekNumber]; //Week number. int i; for (i = 1; i < 8; i=i+1){ [comp setWeekday:i]; //First day of the week. Change it to 7 to get the last date of the week NSDate *resultDate = [gregorian dateFromComponents:comp]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"EEEE MMMM d, yyyy"]; NSString *myDateString = [formatter stringFromDate:resultDate]; NSLog(@"%@",myDateString); } 

使用NSDateComponents找出星期几,然后find它之前和之后所需的date。

嗯,我不是这方面的专家或者其他方面的专家,但是一个愚蠢的做法是首先能够从NSDate识别出一周中的哪一天。 然后你可以添加if语句,然后if语句可以添加和减去date和nslogprintf每个。 或者我认为通过访问ios日历有一种方法。

好吧,所以上面的说得很多,做得比我说的还多。 是的,去检查我之前的post上的链接。

使用NSCalendar实例将您的启动NSDate实例转换为NSDateComponents实例,将1天添加到NSDateComponents并使用NSCalendar将其转换回NSDate实例。 重复最后两个步骤,直到到达结束date。

 NSDate *currentDate = [NSDate date]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setDay:1]; NSDate *nextDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0]; 

或者find开始date和结束date。 喜欢

 NSDate *startDate = [NSDate date]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setDay:7]; NSDate *endDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0]; 

那么,NSDate nextDate;

 for ( nextDate = startDate ; [nextDate compare:endDate] < 0 ; nextDate = [nextDate addTimeInterval:24*60*60] ) { // use date }