工作日的NSDate转换设置

时间:2012-11-26 05:49:34

标签: objective-c ios cocoa-touch

我有一个NSDate对象*myDate。我可以从这个日期开始工作日,我想将工作日设置为integer c并仅在该周获取新的日期对象。

2 个答案:

答案 0 :(得分:2)

我搜索了它,我找到了答案,这里是代码..

  NSDateComponents *comps = [[NSDateComponents alloc] init];
                [comps setDay:1];
             NSDate   *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:[NSDate date] options:0];
                NSLog(@"new date==%@",newDate);

这将返回第二天的日期。

答案 1 :(得分:1)

 NSDate *date = [NSDate date];
            NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
            [formatter setDateFormat:@"MMM dd, yyy"];
            date = [formatter dateFromString:string];

            NSCalendar *calendar = [NSCalendar currentCalendar];
            NSInteger units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |       NSWeekdayCalendarUnit;
            NSDateComponents *components = [calendar components:units fromDate:date];
            NSInteger day = [components day];
            NSInteger weekday = [components weekday]; // if necessary