从nsdate得到一周中的任何一天?

时间:2012-03-30 06:49:44

标签: iphone objective-c ios

  

可能重复:
  iPhone NSDate eg. next Friday

我有一个日期假设今天的日期,现在我想要相应日期的星期几或星期一和相应周的星期六的所有数组我怎么能这样做? -Thanx

2 个答案:

答案 0 :(得分:2)

试试此代码

NSMutableArray *weekdays = [[NSMutableArray alloc] init];
NSDate *today = [NSDate date]; 
NSDateComponents *comp = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:today];
for (int i=1; i <= 7 ;i++) 
{

    [weekdays addObject:[today dateByAddingTimeInterval:((i-[comp weekday])*24*60*60)]];
}
NSLog(@"%@",weekdays);
NSLog(@"Monday :%@ StaturDay:%@",[weekdays objectAtIndex:1],[weekdays objectAtIndex:6]);

答案 1 :(得分:1)

我正在使用以下代码。试试这个

NSMutableArray *daysArray;
        NSString *weekdayStr;
        NSMutableArray *weekdayArray;

weekdayArray = [[NSMutableArray alloc]init];
    daysArray = [[NSMutableArray alloc]init ];
    NSDate *currentDate = [NSDate date];
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

        NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
        [inputFormatter setDateFormat:@"yyyy-MM-dd"];

    NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit) fromDate:currentDate];

    int month ;
    int year ;
    //int weekDay ;
    int day ;

    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit |NSHourCalendarUnit | NSMinuteCalendarUnit;   
    for (int i = 0; i < 7; i++ ) {
        day = [weekdayComponents day]-i;
        month = [weekdayComponents month];
        year = [weekdayComponents year];
    //  weekDay = [weekdayComponents weekday]-i;

        NSDateComponents *compsTime = [gregorian components:unitFlags fromDate:[NSDate date]];
        [compsTime setDay:day];
        [compsTime setMonth:month];
        [compsTime setYear:year];

        NSString *tempString = [inputFormatter stringFromDate:[gregorian dateFromComponents:compsTime]];
        NSArray *separateString=[tempString componentsSeparatedByString:@" "];
        NSString *date=[separateString objectAtIndex:0];

        [daysArray addObject:date];

        NSDateComponents *weekdayComponents1 = [gregorian components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit) fromDate:[gregorian dateFromComponents:compsTime]];
        int weekDay1 = [weekdayComponents1 weekday];

        switch (weekDay1) {
            case 1:
                weekdayStr = @"(Sunday)";
                break;
            case 2:
                weekdayStr = @"(Monday)";
                break;
            case 3:
                weekdayStr = @"(Tuesday)";
                break;
            case 4:
                weekdayStr = @"(Wednesday)";
                break;

            case 5:
                weekdayStr = @"(Thursday)";
                break;

            case 6:
                weekdayStr = @"(Friday)";
                break;

            case 7:
                weekdayStr = @"(Saturday)";
                break;

            default:
                break;
        }
        NSLog(@"weekDay %@ date: %@",weekdayStr,tempString);
         [weekdayArray addObject:[NSString stringWithFormat:@"%d",weekDay1]];
    }
    NSLog(@"days array: %@", weekdayArray)