1

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

4

2 回答 2

2

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

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

这将返回第二天的日期。

于 2012-11-26T06:58:00.157 回答
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
于 2012-11-26T06:18:14.810 回答