3

我想创建一个函数,结果是下周五的日期,但我没有计划如何去做。有没有人给我一个好的提示?

4

5 回答 5

2

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

于 2010-05-22T00:26:06.943 回答
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];
于 2011-09-21T08:49:53.807 回答
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;

}

于 2013-02-12T14:42:33.360 回答
0

保持简单、安全和可读!(....基萨尔?)

#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在一天后初始化(因此,如果当前日期是星期五,它将从星期六开始,强制下星期五。如果当前日期是星期四,您将自动获得下一个星期五而无需循环) .

于 2013-07-18T16:49:17.957 回答
-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];
于 2010-05-24T20:13:36.000 回答