如何从iphone/ipad当前日期开始查找第三天的日期

如何从iphone/ipad当前日期开始查找第三天的日期,iphone,ipad,datetime,Iphone,Ipad,Datetime,有人能告诉我如何从iphone/ipad的当前日期中找出第三天的日期吗 提前感谢类似于: NSDate *today = [NSDate date]; NSTimeInterval threeDays = 86400*3; NSDate *threeDaysFromToday = [NSDate dateWithTimeInterval:threeDays sinceDate:today]; // get the gregorian calendar ready to go; use the

有人能告诉我如何从iphone/ipad的当前日期中找出第三天的日期吗

提前感谢

类似于:

NSDate *today = [NSDate date];
NSTimeInterval threeDays = 86400*3;

NSDate *threeDaysFromToday = [NSDate dateWithTimeInterval:threeDays sinceDate:today];
// get the gregorian calendar ready to go; use the getter for the current system
// calendar if you're happy to deal with other calendars too
NSCalendar *gregorianCalendar = [[NSCalendar alloc]
                                initWithCalendarIdentifier: NSGregorianCalendar];

// get an NSDate object that is three days in the future
NSDate *dateInFuture = [NSDate dateWithTimeIntervalSinceNow:3*24*60*60];

// grab the date components for the bits we want to print in this example
NSDateComponents *componentsOfDateInFuture = 
       [gregorianCalendar
        components:NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit
          fromDate:dateInFuture];

// and print
NSLog(@"in three days it'll be %04d/%02d/%02d", 
           componentsOfDateInFuture.year,
           componentsOfDateInFuture.month, 
           componentsOfDateInFuture.day);

// clean up
[gregorianCalendar release];
编辑:考虑到夏令时的问题,巴勃罗·圣克鲁斯的答案更适合于确保你在未来三天的工作时间。这是您将NSDate分解为日/月/年的方式,目的是获得日期而不是简单的NSDate。

类似于:

// get the gregorian calendar ready to go; use the getter for the current system
// calendar if you're happy to deal with other calendars too
NSCalendar *gregorianCalendar = [[NSCalendar alloc]
                                initWithCalendarIdentifier: NSGregorianCalendar];

// get an NSDate object that is three days in the future
NSDate *dateInFuture = [NSDate dateWithTimeIntervalSinceNow:3*24*60*60];

// grab the date components for the bits we want to print in this example
NSDateComponents *componentsOfDateInFuture = 
       [gregorianCalendar
        components:NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit
          fromDate:dateInFuture];

// and print
NSLog(@"in three days it'll be %04d/%02d/%02d", 
           componentsOfDateInFuture.year,
           componentsOfDateInFuture.month, 
           componentsOfDateInFuture.day);

// clean up
[gregorianCalendar release];
编辑:考虑到夏令时的问题,巴勃罗·圣克鲁斯的答案更适合于确保你在未来三天的工作时间。这是您将NSDate分解为日/月/年的方式,目的是获得日期而不是简单的NSDate。

选择一个:

NSDate *futureDate;
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setDay:3];
futureDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:[NSDate date] options:0];
NSLog(@"%@ - %@", [NSDate date], futureDate);



NSDate *futureDate = [[NSDate date] dateByAddingTimeInterval:3 * (24*60*60)];
选择一个:

NSDate *futureDate;
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setDay:3];
futureDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:[NSDate date] options:0];
NSLog(@"%@ - %@", [NSDate date], futureDate);



NSDate *futureDate = [[NSDate date] dateByAddingTimeInterval:3 * (24*60*60)];
您可以使用以下选项:

NSDate *today = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:3];
NSDate *threeDaysFromToday = [gregorian dateByAddingComponents:offsetComponents toDate:today options:0];
从中稍微修改示例。有关更多信息和示例,请查看链接。

您可以使用以下方法:

NSDate *today = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:3];
NSDate *threeDaysFromToday = [gregorian dateByAddingComponents:offsetComponents toDate:today options:0];
[NSDate dateWithTimeIntervalSinceNow:86400*3]
从中稍微修改示例。查看链接了解更多信息和示例

[NSDate dateWithTimeIntervalSinceNow:86400*3]
但正如上面提到的,DST可能会使这太不准确。这取决于它的重要性

但正如上面提到的,DST可能会使这太不准确。取决于它的重要性。

以下是示例代码

    NSDate  * currentDate=[NSDate date];
NSTimeInterval interval=[currentDate timeIntervalSince1970];
NSTimeInterval intervalForThirdDate=interval+86400*3;
NSDate *nextDate=[[NSDate alloc]initWithTimeIntervalSince1970:intervalForThirdDate];
下面是示例代码

    NSDate  * currentDate=[NSDate date];
NSTimeInterval interval=[currentDate timeIntervalSince1970];
NSTimeInterval intervalForThirdDate=interval+86400*3;
NSDate *nextDate=[[NSDate alloc]initWithTimeIntervalSince1970:intervalForThirdDate];

应该在夏令时小心使用。谢谢Christo,你的答案是静态的,所以我尝试了Louis Gerbarg的帖子中的这个示例-在下面的url=谢谢Christo应该在夏令时小心使用。谢谢Christo,你的答案是静态的,所以我尝试了Louis Gerbarg的帖子中的这个示例-在下面的url=谢谢CristoI我不确定你的意思是将间隔乘以86400;1970年后的约会时间是现在的86400倍。据我所知,他/她没有这样做,是吗?他把86400*3加到间隔上。不过,对于他所采取的方法来说,这个例子似乎有点复杂。我不确定你的意思是将间隔乘以86400;1970年后的约会时间是现在的86400倍。据我所知,他/她没有这样做,是吗?他把86400*3加到间隔上。然而,对于他所采取的方法来说,这个例子似乎有点复杂。