iOS 日期处理方法

获取需要增加或减少的年、月、日即可获得新的日期,上述的表示获取mydate日期前一个月的日期

- (NSDate *)getLaterDateFromDate:(NSDate *)date withYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
        NSDateComponents *comps = nil;
    
        comps = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
    
        NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    
         [adcomps setYear:year];
    
         [adcomps setMonth:month];
    
         [adcomps setDay:day];
    
         NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:date options:0];
    
         return newdate;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 关于Mongodb的全面总结 MongoDB的内部构造《MongoDB The Definitive Guide》...
    中v中阅读 32,038评论 2 89
  • 所幸,在我最好的年华里遇上最好的你。抬头望天空,你是最亮的那颗星星。 不知道为什么,我总觉得该提笔写些...
    青陵1阅读 263评论 0 0
  • 浩仁阅读 215评论 0 0
  • 你在你的旅途上行走;我在我的路上前行。 地球是很大但总有一天会相遇#
    一与一阅读 214评论 0 0
  • -1- 这阵子,慕容身边有个朋友失恋了,一直在闹情绪,慕容和身边几个很好的朋友一直陪他说话,希望他能快点走出来。 ...
    慕容匪我阅读 592评论 13 12