开发者

iPhone NSDate eg. next Friday

开发者 https://www.devze.com 2022-12-31 11:56 出处:网络
I want to create a function which results the dat开发者_高级运维e of next Friday but I have no plan how to do it. Has anyone a good hint to me ?E.g. Get current date using NSDate, then use \'component

I want to create a function which results the dat开发者_高级运维e of next Friday but I have no plan how to do it. Has anyone a good hint to me ?


E.g. Get current date using NSDate, then use 'components>fromDate:' from NSCalendar to get the NSDateComponents, then add the time difference to next Friday and create a new NSDate and Bob's is your uncle.


Here is my working solution for getting the next 5 Sundays in Gregorian calendar:

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];


This should work

+ (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;

}


Keep it simple, safe, and readable! (....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];
}

Now nextFriday has your date.

Hope this helps!

EDIT Note that if the current date was already a Friday, it would return that instead of the next Friday. If that's undesirable just init your nextFriday to a day later (so if current date was a Friday, it would start on Saturday, forcing the next Friday. And if current date was a Thursday you'd automatically have your next Friday without needing the loop).


Here is my solution, and just to warn you, on a saturday is the friday before shown. cheers to all

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号