2009-07-04 36 views

Trả lời

57

Trong phần sau, yourDate đại diện cho NSDate đầu vào của bạn; nextDate đại diện cho ngày hôm sau.

// 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

Xin chào, Có mã này đang hoạt động Cảm ơn rất nhiều, Bạn có ý tưởng gì về cách để có được ngày hôm trước (hôm qua) không? – Jai

+0

Thay đổi [offsetComponents setDay: 1] thành setDay: -1 kết quả trừ đi một ngày thay vì thêm – rpetrich

+0

Điều này cũng hoạt động khi ngày tiếp theo chuyển từ tiết kiệm ban ngày sang thời gian chuẩn. Hãy thử di chuyển từ 2011-11-06. Cảm ơn @unforgiven. – johnnieb

2

// Thêm mã dưới đây để init phương pháp hoặc viewDidLoad

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

// Thêm mã này, nơi bạn muốn lấy số ngày tiếp theo hoặc trước đó

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

Cave: Lặp lại thêm giâyPerDay (24 * 60 * 60) với addTimeInterval vào một NSDate được thực hiện trong khi quan sát thời gian tiết kiệm ánh sáng ban ngày. Thêm 24 giờ vào một ngày khi bạn nhập hoặc rời khỏi thời gian savin ban ngày sẽ làm lệch thời gian trong ngày bằng +/- 1 giờ. –

28

gì về phương pháp này?

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

** Nhiều hơn ** giải pháp tốt hơn so với các upvoted nhất trong chủ đề này –

3

Tôi đoán phương pháp này chỉ hoạt động tốt đối với tôi.

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

Điều này có thể gây ra vấn đề khi đi qua ranh giới ngày khi kế toán cho DST. – Kyle

+2

Ditto, Điều này có thể cho kết quả không chính xác khi có thay đổi Thời gian tiết kiệm ánh sáng ban ngày trong phạm vi ngày. – progrmr

+0

Sẽ không làm những gì bạn mong đợi nếu có giây nhảy vọt. –

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

nhiều hơn Cách đơn giản nhất ..

+19

Điều này có thể cho kết quả không chính xác khi có thay đổi Thời gian tiết kiệm ánh sáng ban ngày trong phạm vi ngày. – progrmr

+4

Ngoài ra khi có giây nhảy vọt. –

+0

Đủ tốt cho tôi –

Các vấn đề liên quan