thể trùng lặp:
iOS and finding TomorrowLàm thế nào tôi có thể nhận được ngày tiếp theo bằng cách sử dụng NSDate?
Làm thế nào tôi có thể có được ngày tiếp theo sử dụng NSDate. Xin vui lòng gửi cho tôi giải pháp.
thể trùng lặp:
iOS and finding TomorrowLàm thế nào tôi có thể nhận được ngày tiếp theo bằng cách sử dụng NSDate?
Làm thế nào tôi có thể có được ngày tiếp theo sử dụng NSDate. Xin vui lòng gửi cho tôi giải pháp.
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];
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
Thay đổi [offsetComponents setDay: 1] thành setDay: -1 kết quả trừ đi một ngày thay vì thêm – rpetrich
Đ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
// 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"];
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ờ. –
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);
** Nhiều hơn ** giải pháp tốt hơn so với các upvoted nhất trong chủ đề này –
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];
NSDate *tomorrow = [NSDate dateWithTimeInterval:(24*60*60) sinceDate:[NSDate date]];
nhiều hơn Cách đơn giản nhất ..
Đ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
Ngoài ra khi có giây nhảy vọt. –
Đủ tốt cho tôi –
Bạn nên đánh dấu một trong những câu trả lời dưới đây chính xác nếu bạn cảm thấy nó trả lời câu hỏi của bạn. –