zoukankan      html  css  js  c++  java
  • 【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];
  • 相关阅读:
    [转]Asp.Net 备份和恢复SQL SERVER 数据库
    alert 的封装
    using(sqlConnection conn=new sqlConnection) 中using的作用
    dotnet 上传大文件的配置的方法
    allowDefinition='MachineToApplication'
    转 Server Application Error报错信息的解决方案
    url 自动加入链接
    MVC中使用RadioButtonFor
    linux iptables squid 透明代理
    linux iptables网关配置,端口转发
  • 原文地址:https://www.cnblogs.com/kaysun/p/5519222.html
Copyright © 2011-2022 走看看