一、给一个时间,给一个数,正数是以后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. Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。

    You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...

  2. WPF-Binding的源

    1. 绑定到其它元素 <Grid> <StackPanel> <TextBox x:Name="textbox1" /> <Label C ...

  3. Android-ViewPagerIndicator框架使用——使用概要

    概要:关于ViewPagerIndicator这个框架,我这里只讲解如何使用,而不去讲解他是如何实现的,所以想了解源码剖析的朋友,这个就可以略过了. ViewPagerIndicator这个框架通过自 ...

  4. poj 3189(枚举+多重匹配)

    题目链接:http://poj.org/problem?id=3189 思路:由于题目要求最小的差值,而Range最多也才20,因此我们可以枚举上下限,多重匹配验证即可. http://paste.u ...

  5. [Unity热更新]tolua# & LuaFramework(一):基础

    一.tolua# c#调用lua:LuaState[变量名/函数名] 1.LuaState a.执行lua代码段 DoString(string) DoFile(.lua文件名) Require(.l ...

  6. ipod锁定后的恢复

    1.断开 USB 线缆与设备的连接,但保持线缆的另一端与电脑的 USB 端口相连. 2.关闭设备:按住“睡眠/唤醒”按钮数秒,直到出现红色滑块,然后滑动该滑块.等待设备关闭. 3.按住主屏幕按钮,同时 ...

  7. 《从零开始学Swift》学习笔记(Day 28)——总结使用问号(?)和感叹号(!)

    原创文章,欢迎转载.转载请注明:关东升的博客 在使用可选类型和可选链时,多次使用了问号(?)和感叹号(!),但是它们的含义是不同的,下面我来详细说明一下. 1. 可选类型中的问号(?) 声明这个类型是 ...

  8. 检测当前的语言环境是否使用了 UTF-8 编码(三篇文章:先用setlocale()设置编码,再用nl_langinfo()进行检测。locale对象可以使用langLocale.name() == "zh_CN"判断)

    C/C++程序中,locale(即系统区域设置,即国家或地区设置)将决定程序所使用的当前语言编码.日期格式.数字格式及其它与区域有关的设置,locale设置的正确与否将影响到程序中字符串处理(wcha ...

  9. Java性能监控之javassist探索

    https://www.cnblogs.com/orionhp/p/6362615.html ApplicationContext实现的默认行为就是在启动时将所有singleton bean提前进行实 ...

  10. (2)linux未使用eth0,未使用IPV4导致无法连接

    首先ifconfig查看网络IP 看,我这里默认启用了2个网卡,一个是eth0,另一个是lo(基于loopback方式) 1.如果有eth0则做:界面修改 (1)输入命令setup,选择network ...