ios 如何使用 NSDate 获取下一个日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1081689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:07:03  来源:igfitidea点击:

How can i get next date using NSDate?

iphoneiosnsdate

提问by Jai

Possible Duplicate:
iOS and finding Tomorrow

可能的重复:
iOS 和寻找明天

How can i get next date using NSDate. Please send me the solution.

如何使用 NSDate 获取下一个日期。请把解决方案发给我。

回答by Massimo Cafaro

In the following, yourDate represents your input NSDate; nextDate represents the next day.

在下面,yourDate 代表你输入的 NSDate;nextDate 代表第二天。

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

回答by Paresh Rathod

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

Much more easiest way..

更简单的方法..

回答by S.P.

what about this methods ?

这个方法怎么样?

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

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);

回答by Vanya

I guess this method works just fine for me.

我想这个方法对我来说很好用。

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

回答by Jai

//Add the below code to init method or viewDidload

//将下面的代码添加到init方法或viewDidload

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

//Add this code where you wanna retrieve next or previous dates

//将此代码添加到您要检索下一个或上一个日期的位置

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