Has*_*tha 6 cocoa objective-c datetimeoffset
我想在第二天给出当前日期我使用的代码如下
+(NSDate *)getForDays:(int)days fromDate:(NSDate *) date {
    NSTimeInterval secondsPerDay = 24 * 60 * 60 * days;
        return [date addTimeInterval:secondsPerDay];
} 
这工作正常,但启用夏令时会导致错误.如何在启用夏令时时使其工作.
Mik*_*lah 13
正如您所发现的,您现在拥有的内容非常容易出错.它不仅可以在夏令时变化,而且如果您的用户有非格里历日历会怎样?然后,天不是24小时.
相反,使用NSCalendar和NSDateComponents正是为此设计的:
+ (NSDate *)getForDays:(int)days fromDate:(NSDate *)date
{
    NSDateComponents *components= [[NSDateComponents alloc] init];
    [components setDay:days];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    return [calendar dateByAddingComponents:components toDate:date options:0];
}