1

我想用日期名称获取当前和下周的日期。

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

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

我怎样才能做到这一点?

谢谢,

4

5 回答 5

12

这是可以解决问题的示例代码。

-(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];
}
于 2013-06-28T07:33:00.507 回答
1

我打算复制并粘贴一个答案,但我建议去阅读这篇文章:当前周开始和结束日期。看起来它会帮助你实现你想要的。

可能是这样的:

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);

}
于 2013-06-28T07:04:49.540 回答
0

用于NSDateComponents找出星期几,然后找出它之前和之后所需的日期。

于 2013-06-28T06:58:20.897 回答
0

嗯,我不是这方面或任何方面的专家,但一种愚蠢的做法是首先能够从NSDate. 然后您可以添加 inif语句,然后 if 语句可以添加和减去日期和nslog/或printf每个日期。或者我认为有一种方法可以访问 ios 日历。

好的,所以上面说的和做的比我说的要多。是的,去我之前查看帖子上的链接。

于 2013-06-28T07:07:36.840 回答
0

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

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];

或者找到开始日期和结束日期。喜欢

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 下一个日期;

for ( nextDate = startDate ; [nextDate compare:endDate] < 0 ; nextDate = [nextDate addTimeInterval:24*60*60] ) {
  // use date
}
于 2013-06-28T07:20:36.617 回答