• 【ios】 日期单独对年、月、日做加减


    1、当前时间减去18年

         NSDate *currentDate = currentDate = [NSDate date];
         NSCalendar
    *calendar = nil; if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) { calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian]; } else { calendar = [NSCalendar currentCalendar]; } NSDateComponents *dateComponents = [calendar components:NSYearCalendarUnit fromDate:currentDate]; [dateComponents setYear:-18]; NSDate *newdate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0];

    2、当前时间减去18年4个月12天

         NSDate *currentDate = currentDate = [NSDate date];
         NSCalendar *calendar = nil;
            if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {
                calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
            } else {
                calendar = [NSCalendar currentCalendar];
            }
            NSDateComponents *dateComponents = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:currentDate];
            [dateComponents setYear:-18];
            [dateComponents setMonth:-4];
            [dateComponents setDay:-12];
            
            NSDate *newdate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0];   

    3、当前时间加上18年4个月12天

         NSDate *currentDate = currentDate = [NSDate date];
         NSCalendar *calendar = nil;
            if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {
                calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
            } else {
                calendar = [NSCalendar currentCalendar];
            }
            NSDateComponents *dateComponents = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:currentDate];
            [dateComponents setYear:18];
            [dateComponents setMonth:4];
            [dateComponents setDay:12];
            
            NSDate *newdate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0];
  • 相关阅读:
    如何将本地代码上传到GitHub
    《剑指offer》JavaScript版19-21题
    《剑指offer》JavaScript版16-18题
    《剑指offer》JavaScript版13-15题
    蘑菇街(前端1面)
    《二十二》观后感
    前端面试题(2)
    《深入理解ES6》之Promise
    HDU 4939 Stupid Tower Defense (2014 Multi-University Training Contest 7)
    HDU 4940 Destroy Transportation system(2014 Multi-University Training Contest 7)
  • 原文地址:https://www.cnblogs.com/kaysun/p/5519222.html
Copyright © 2020-2023  润新知