iPhone NSDate例如。下周五

时间:2010-05-21 23:35:42

标签: objective-c iphone nsdate nscalendar

我想创建一个导致下周五日期的函数,但我没有计划如何去做。有人对我有好心吗?

5 个答案:

答案 0 :(得分:2)

E.g。使用NSDate获取当前日期,然后使用NSCalendar中的'components> fromDate:'来获取NSDateComponents,然后将时间差添加到下周五并创建一个新的NSDate,Bob就是你的叔叔。

答案 1 :(得分:2)

以下是我在格里高利历中接下来的5个星期日的工作解决方案:

self.nextBeginDates = [NSMutableArray array];

NSDateComponents *weekdayComponents = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
int currentWeekday = [weekdayComponents weekday]; //[1;7] ... 1 is sunday, 7 is saturday in gregorian calendar

NSDateComponents *comp = [[NSDateComponents alloc] init];
[comp setDay:8 - currentWeekday];   // add some days so it will become sunday

// build weeks array
int weeksCount = 5;
for (int i = 0; i < weeksCount; i++) {
    [comp setWeek:i];   // add weeks

    [nextBeginDates addObject:[[NSCalendar currentCalendar] dateByAddingComponents:comp toDate:[NSDate date] options:0]];
}
[comp release];

答案 2 :(得分:1)

这应该有效

+ (NSDate *) dateForNextWeekday: (NSInteger)weekday {

NSDate *today = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];

// Get the weekday component of the current date
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit
                                                   fromDate:today];

/*
 Add components to get to the weekday we want
 */
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
NSInteger dif = weekday-weekdayComponents.weekday;
if (dif<=0) dif += 7;
[componentsToSubtract setDay:dif];

NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract
                                                     toDate:today options:0];

return beginningOfWeek;

}

答案 3 :(得分:0)

保持简单,安全和可读! (.... KISSAR?)

#define FRIDAY 6

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = 1;

NSDate *nextFriday = [NSDate date];
NSInteger iWeekday = [[gregorian components:NSWeekdayCalendarUnit fromDate:nextFriday] weekday];

while (iWeekday != FRIDAY) {
    nextFriday = [gregorian dateByAddingComponents:dayComponent toDate:nextFriday options:0];
    iWeekday = [[gregorian components:NSWeekdayCalendarUnit fromDate:nextFriday] weekday];
}

现在nextFriday有您的约会对象。

希望这有帮助!

修改 请注意,如果当前日期已经是星期五,它将返回而不是下周五。如果这是不可取的,只需将你的nextFriday提升到一天(所以如果当前日期是星期五,它将在星期六开始,迫使下周五。如果当前日期是星期四,你将自动拥有你的下个星期五不需要循环)。

答案 4 :(得分:-2)

这是我的解决方案,只是为了警告你,星期六是星期五之前显示的。 欢呼所有人

NSDate *today = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:today];
int weekday = [weekdayComponents weekday];


int iOffsetToFryday = -weekday + 6;
weekdayComponents.weekday = iOffsetToFryday;

NSDate *nextFriday = [[NSCalendar currentCalendar] dateByAddingComponents:weekdayComponents toDate:today options:0];