美文网首页
计算时间差--NSDate对时间的换算

计算时间差--NSDate对时间的换算

作者: 十指恋静 | 来源:发表于2016-08-01 17:17 被阅读49次

获得一个月后 的日期 该如何实现?

    NSCalendar * calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];// 公立
    NSDateComponents * components = [[NSDateComponents alloc]init];
    
    [components setYear: 1];
    [components setMonth: - 1];
    [components setDay: 5]; //设置你需要增加或减少的年、月、日即可获得新的日期
    
    NSDate * newData = [calendar dateByAddingComponents:components toDate:[NSDate date] options:0];
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"YY年MM月dd日"];
    NSString * timeString = [dateFormatter stringFromDate:newData];
    
    NSLog(@"更改后的日期是:%@", timeString);

相关文章

网友评论

      本文标题:计算时间差--NSDate对时间的换算

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