Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 获取特定日期-目标c_Iphone_Objective C_Date - Fatal编程技术网

Iphone 获取特定日期-目标c

Iphone 获取特定日期-目标c,iphone,objective-c,date,Iphone,Objective C,Date,我想在objective C-iphone中获得从今天起45天内的日期。 我是目标C的新手:) 我知道我可以做一些事情,比如: NSString *dateStr = @"Tue, 16 April 2013 13:00:00 +0000"; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"EE, d LLLL yyyy HH:mm:ss Z"]; NSDate *

我想在objective C-iphone中获得从今天起45天内的日期。 我是目标C的新手:)

我知道我可以做一些事情,比如:

NSString *dateStr = @"Tue, 16 April 2013 13:00:00 +0000";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EE, d LLLL yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr]; 
[dateFormat release];
但是,我不想静态分配。。。它应该是动态的。 每天我都需要进入
*date
变量从今天起45天的日期

试着这样做:

NSDate *today=[NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components=[[NSDateComponents alloc] init];
components.day=45;
NSDate *targetDate =[calendar dateByAddingComponents:components toDate:today options: 0];

看这里@George,很好,谢谢。我试试。什么是静态的,什么是动态的<代码>[NSDate date]始终是动态的,返回当前日期。@乔治:这不是一个好的解决方案,在那里是可以接受的。它不关心日光,闰秒,我的意思是,我需要从今天起45天的日期,每天!不是从今天起45天的日期