2010-05-21 6 views
3

Je veux créer une fonction qui aboutit à la date de vendredi prochain mais je n'ai aucun plan comment le faire. Quelqu'un at-il un bon indice pour moi?iPhone NSDate par ex. prochain vendredi

+1

Le [Ajout de composants à une date] (http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html#//apple_ref/doc/uid/TP40007836- SW3) dans le Guide de programmation de la date et de l'heure sera utile. Le listing 2 contient du code pour trouver le dimanche de la semaine en cours. Modifiez cela pour trouver le vendredi suivant à la place. – DyingCactus

Répondre

2

E.g. Obtenez la date actuelle en utilisant NSDate, puis utilisez 'components> fromDate:' de NSCalendar pour obtenir NSDateComponents, puis ajoutez le décalage horaire à vendredi prochain et créez un nouveau NSDate et Bob est votre oncle.

+0

Je vais essayer de comprendre avec cette route. Bravo –

-2

Voici ma solution, et juste pour vous avertir, un samedi est le vendredi avant montré. acclamations à tous

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

Comment pouvez-vous considérer cela comme correct s'il renvoie la mauvaise réponse lors d'un samedi? –

+0

je vois que ce n'est pas la bonne façon de gérer –

2

Voici ma solution de travail pour obtenir les 5 prochains dimanches calendrier grégorien:

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

Cela devrait fonctionner

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

}

+0

Il ya effectivement un bug dans ce cas en fonction des exigences de la question. Si vous essayez d'obtenir la prochaine instance du jour de la semaine en cours (c'est-à-dire vendredi prochain un vendredi), elle finira par vous donner la date actuelle. La solution serait de changer 'dif <0'' 'dif <= 0'. – Dima

0

Gardez simple, sûr et lisible! (.... 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]; 
} 

maintenant nextFriday a votre date.

Espérons que cela aide!

EDIT Notez que si la date du jour était déjà un vendredi, elle retournera cette valeur au lieu du vendredi suivant. Si cela n'est pas souhaitable, initez votre nextFriday un jour plus tard (donc si la date actuelle était un vendredi, elle commencerait samedi, forçant le vendredi suivant.) Si la date actuelle était un jeudi, vous auriez automatiquement votre prochain vendredi sans avoir besoin de la boucle).