2009-07-04 12 views

Odpowiedz

57

W dalszej części twoja data reprezentuje twoje wejście NSDate; nextDate reprezentuje następny dzień.

// start by retrieving day, weekday, month and year components for yourDate 
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:yourDate]; 
    NSInteger theDay = [todayComponents day]; 
    NSInteger theMonth = [todayComponents month]; 
    NSInteger theYear = [todayComponents year]; 

    // now build a NSDate object for yourDate using these components 
    NSDateComponents *components = [[NSDateComponents alloc] init]; 
    [components setDay:theDay]; 
    [components setMonth:theMonth]; 
    [components setYear:theYear]; 
    NSDate *thisDate = [gregorian dateFromComponents:components]; 
    [components release]; 

    // now build a NSDate object for the next day 
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; 
    [offsetComponents setDay:1]; 
    NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0]; 
    [offsetComponents release]; 
    [gregorian release]; 
+0

Witam, Tak, ten kod działa Wielkie dzięki, Czy masz pojęcie o tym, jak uzyskać poprzedni dzień (wczoraj)? – Jai

+0

Zmiana [offsetComponents setDay: 1] do ustawienia date: -1 powoduje odjęcie dnia zamiast dodania – rpetrich

+0

Działa to również, gdy następna data przechodzi od czasu letniego do standardowego. Spróbuj przejść od 2011-11-06. Dzięki @unforgiven. – johnnieb

2

// Dodaj poniższy kod do metody init lub viewDidLoad

[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"Date"]; 

// dodaj ten kod tam gdzie chcesz odzyskać następne lub poprzednie dat

NSDate *tomorrow = [[NSUserDefaults standardUserDefaults] valueForKey:@"Date"]; 
NSTimeInterval secondsPerDay = 24 * 60 * 60;  
NSDate *date = [tomorrow addTimeInterval:secondsPerDay]; //Change NSDate *date = [tomorrow addTimeInterval:-secondsPerDay]; for yesterday 
tomorrow = date; 
[self setDate:tomorrow]; 
dateTextField.text = [self getDate];   
[[NSUserDefaults standardUserDefaults] setObject:tomorrow forKey:@"Date"]; 
+1

Jaskinia: wielokrotne dodawanie sekundPerDay (24 * 60 * 60) z addTimeInterval do NSDate odbywa się przy zachowaniu czasu letniego. Dodanie 24 godzin w dniu, w którym wchodzisz lub wychodzisz z trybu dziennego, pochyla godzinę o +/- 1 godzinę. –

28

co o tej metody?

http://www.drobnik.com/touch/2009/05/adding-days-to-nsdate/

NSDate *now = [NSDate date]; 
int daysToAdd = 50; // or 60 :-) 

// set up date components 
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; 
[components setDay:daysToAdd]; 

// create a calendar 
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 

NSDate *newDate2 = [gregorian dateByAddingComponents:components toDate:now options:0]; 
NSLog(@"Clean: %@", newDate2); 
+1

** Wiele ** lepsze rozwiązanie niż najbardziej wznowione w tym temacie. –

3

Chyba ta metoda działa dobrze dla mnie.

NSString *zajtra = [[NSDate date] dateByAddingTimeInterval:86400]; 
+1

Może to powodować problemy podczas przekraczania granic dnia podczas rozliczania DST. – Kyle

+2

Ditto, Może to dawać niepoprawne wyniki w przypadku zmiany czasu letniego w zakresie dat. – progrmr

+0

Nie zrobi tego, czego się spodziewasz, jeśli upłynie sekunda. –

34
NSDate *tomorrow = [NSDate dateWithTimeInterval:(24*60*60) sinceDate:[NSDate date]]; 

Znacznie więcej najprostszy sposób ..

+19

Może to dawać niepoprawne wyniki w przypadku zmiany czasu letniego w zakresie dat. – progrmr

+4

Również w przypadku sekundy przestępnej. –

+0

Dobra robota dla mnie –

Powiązane problemy