IPhone NSDate, for example. the next Friday

I want to create a function that will lead to the next Friday date, but I have no plan on how to do this. Does anyone have a good hint on me?

+3
source share
5 answers

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

+2
source

Here is my working solution for getting the following 5 Sundays on the 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];
+2
source

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

}

+1

, ! (.... 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 ( , , , . , ).

0

, , , .

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

Source: https://habr.com/ru/post/1746642/


All Articles