一、给一个时间,给一个数,正数是以后n个月,负数是前n个月;

 -(NSDate *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(NSInteger)month

 {

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

     [comps setMonth:month];

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

     NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:];

     return mDate;

 }
 
二、设置你需要增加或减少的年、月、日即可获得新的日期,上述的表示获取mydate日期前一个月的日期,如果该成+1,则是一个月以后的日期,以此类推都可以计算。
 - (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:]; return newdate;
}

iOS获取某个日期后n个月的日期的更多相关文章

  1. SQL Server 获取最后一天(指定时间的月最后一天日期)

    /* author OceanHo @ 2015-10-23 10:14:21 获取指定时间字符串指定日期的月最后一天日期 */ IF OBJECT_ID('get_LastDayDate') IS ...

  2. js时间比较,获取n天后(前)的日期

    <html> <head> <meta http-equiv="Content-Type" content="textml; charset ...

  3. iOS获取设备卸载后不变的UUID

    1.首先导入系统库Security.framework 2.创建文件SFHFKeychainUtils.h如下(复制即可): @interface SFHFKeychainUtils : NSObje ...

  4. Laravel Carbon获取 某个时间后N个月的时间

    $time = "2020-11-20 00:00:00"; $res = (new Carbon)->setTimeFromTimeString($time)->ad ...

  5. js获取给定月份的N个月后的日期

    1.在讲js获取给定月份的N个月后的日期之前,小颖先给大家讲下getFullYear().getYear()的区别. ①getYear() var d = new Date() console.log ...

  6. JAVA获取当前日期指定月份后(多少个月后)的日期

    环境要求:使用jdk1.8 package com.date; import java.text.ParseException; import java.text.SimpleDateFormat; ...

  7. python获取当前日期前后N天或N月的日期

    # -*- coding: utf-8 -*- '''获取当前日期前后N天或N月的日期''' from time import strftime, localtime from datetime im ...

  8. iOS 获取公历、农历日期的年月日

    iOS 获取公历.农历日期的年月日 介绍三种方法获取 Date (NSDate) 的年月日. 用 date 表示当前日期.测试日期为公历 2017 年 2 月 5 日,农历丁酉年,鸡年,正月初九. l ...

  9. python里如何获取当前日期前后N天或N月的日期

    #!/usr/bin/python#_*_ coding:UTF-8_*_ import timeimport datetimeimport mathimport calendar ''' time. ...

随机推荐

  1. 【Mac + Appium + Python3.6学习(三)】之IOS自动化测试环境配置

    在做这一节之前先配置我的另一篇文章所需要安装的前提准备条件:<[Mac + Appium学习(一)]之安装Appium环境前提准备> 一.安装IOS自动化测试环境 配置环境: Appium ...

  2. 【Mac + ATX基于uiautomator2】使用weditor时,报错:requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))

    产生以下原因找到了:是因为启动了appium,两者冲突,不能同时使用. 之前讲过怎么安装u2([Mac安装,ATX基于uiautomator2]之安装步骤)以及使用weditor, 但是经过一段时间, ...

  3. Eclipse 运行程序

    运行 Java 程序 我们可以在 Package Explorer 视图 可以在 Package Explorer 视图中快速运行 Java 程序. Package Explorer 视图: 鼠标右击 ...

  4. python 爬虫2 Urllib库的高级用法

    1.设置Headers 有些网站不会同意程序直接用上面的方式进行访问,如果识别有问题,那么站点根本不会响应,所以为了完全模拟浏览器的工作,我们需要设置一些Headers 的属性. import url ...

  5. iOS类目

    首先我们解释一下类目是什么 iOS中类目是为给已经存在的类加入新的方法.(可是不能加入实例变量) 也就是说 我们已经有一个类了 .可是我们发现这个类眼下所提供的方法,满足不了我们的需求,我们须要新的方 ...

  6. js保留几位小数

    function reservedDecimal(val, digit) { return Number(val).toFixed(digit);} 调用 reservedDecimal(10,2); ...

  7. linux c 常常混淆的概念

    指针函数 and 函数指针 指针函数是指带指针的函数,即本质是一个函数.函数都有返回类型(假设不返回值,则为无值型),仅仅只是指针函数返回类型是某一类型的指针. 定义格式 类型名 *函数名(函数參数列 ...

  8. windows Objective-C模拟环境搭建

    安装GNUstep GNUstep Windows Installer提供了Windows平台下的Objective-C的模拟开发环境,一共有四个软件包,其中GNUstep System和GNUste ...

  9. C# 如何判断系统是32位还是64位

    摘自:http://www.cnblogs.com/tom-tong/archive/2012/03/12/2392173.html public static int GetOSBit() { tr ...

  10. log4j分级别打印和如何配置多个Logger

    log4j.rootLogger=dubug,info,warn,error 最关键的是log4j.appender.[level].threshold=[level]   这个是日志分级别打印的最关 ...