美文网首页iOS DeveloperiOS 开发
当前时间往前/往后推几天、几个月、几年

当前时间往前/往后推几天、几个月、几年

作者: 瞎编程 | 来源:发表于2016-07-06 10:14 被阅读400次

//得到当前的时间

NSDate * mydate = [NSDate date];

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy-MM-dd"];

//date转化为string

NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

NSDateComponents *comps = [[NSDateComponents alloc] init];

//正值加日期、负值减日期

[comps setYear:0];

[comps setMonth:-2];

[comps setDay:0];

NSDate *newdate = [calendar dateByAddingComponents:comps toDate:mydate options:0];

NSString *beforDate = [dateFormatter stringFromDate:newdate];

相关文章

网友评论

    本文标题:当前时间往前/往后推几天、几个月、几年

    本文链接:https://www.haomeiwen.com/subject/evhxjttx.html