对于时间的操作在开发中很常见,但有时候我们需要获取到一年后的时间,或者一周后的时间.靠通过秒数计算是不行的.那就牵扯到另外一个日历类(NSCalendar).
下面先简单看一下 NSDate

let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH-mm-ss"
formatter.stringFromDate(date)// 延迟多少秒为正数 前多少秒为负数let
date2 = date.dateByAddingTimeInterval(500)
formatter.stringFromDate(date2)// 返回两个时间较早的时间
let earlierDate = date.earlierDate(date2)// 返回较晚的时间
let laterDate = date.laterDate(date2)

通过上面我们发现计算两天之间的差我们可以推迟3600 *24,一个月呢,有时候30天,有时候31天.那就难以计算了.
下面看看如何使用 NSCalendar.

// 日历获取在9.x之后的系统使用currentCalendar会出异常。在8.0之后使用系统新API。
//let calendar = NSCalendar.currentCalendar()
// 需要传的是一个字符串, 因为日历可以有很多种 这里选择的是公历let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)  // 8.0以后的新方法  /*
日历的类型
@available(iOS, introduced=2.0, deprecated=8.0, message="Use NSCalendarIdentifierGregorian instead")
public let NSGregorianCalendar: String // 公历
@available(iOS, introduced=2.0, deprecated=8.0, message="Use NSCalendarIdentifierBuddhist instead")
public let NSBuddhistCalendar: String // 佛历
@available(iOS, introduced=2.0, deprecated=8.0, message="Use NSCalendarIdentifierChinese instead")
public let NSChineseCalendar: String // 农历
@available(iOS, introduced=2.0, deprecated=8.0, message="Use NSCalendarIdentifierHebrew instead")
public let NSHebrewCalendar: String // 希伯来日历
@available(iOS, introduced=2.0, deprecated=8.0, message="Use NSCalendarIdentifierIslamic instead")
public let NSIslamicCalendar: String // 伊斯兰日历
@available(iOS, introduced=2.0, deprecated=8.0, message="Use NSCalendarIdentifierIslamicCivil instead")
public let NSIslamicCivilCalendar: String // 伊斯兰民事日历
@available(iOS, introduced=2.0, deprecated=8.0, message="Use NSCalendarIdentifierJapanese instead")
public let NSJapaneseCalendar: String // 日本日历
@available(iOS, introduced=4.0, deprecated=8.0, message="Use NSCalendarIdentifierRepublicOfChina instead")
public let NSRepublicOfChinaCalendar: String // 中国日历
@available(iOS, introduced=4.0, deprecated=8.0, message="Use NSCalendarIdentifierPersian instead")
public let NSPersianCalendar: String // 波斯日历
@available(iOS, introduced=4.0, deprecated=8.0, message="Use NSCalendarIdentifierIndian instead")
public let NSIndianCalendar: String // 印度日历
@available(iOS, introduced=4.0, deprecated=8.0, message="Use NSCalendarIdentifierISO8601 instead")  
public let NSISO8601Calendar: String // 国际标准化组织的国际标准日历
*/

有了日历的类我们就方便操作了,这里提供两种方法,
方法一:

// 日期比较的类
let adcomps = NSDateComponents()
adcomps.year = 1adcomps.month = -1adcomps.day = 1// 追加时间可以一次性设置多值
let date5 =  calendar?.dateByAddingComponents(adcomps, toDate: date, options: .WrapComponents)
formatter.stringFromDate(date5!)

注意:

  • .WrapComponents无法进位的.也就是说满一月月份不会加1,而是 Day 循环.什么意思呢.
    比如今天是2016-07-07.只是设置adcomps.day = 24.那么得到的结果是2016-07-31;
    那么如果设置adcomps.day = 25呢,得到的结果不是2016-08-01,而是2016-07-01.也就是做 day 在循环,对于month的设置 效果是一样的.
    如果要进位使用其他的可选值
    在 OC 中直接设置 option 的值为0即可;

方法二:

let date6 = calendar?.dateByAddingUnit(.Year, value: 1, toDate: date, options: .WrapComponents)

相关传送门给大家

https://www.cnblogs.com/isItOk/p/4873051.html

https://www.cnblogs.com/isItOk/p/4619854.html

https://www.jianshu.com/p/e0634fb2a6d2

 

iOS 日历类(NSCalendar)的更多相关文章

  1. NSCalendar 日历类

    NSCalendar 日历类 Cocoa中对日期和时间的处理 NSCalendar (一) (2008-11-12 21:54:10) NSCalendar用于处理时间相关问题.比如比较时间前后.计算 ...

  2. IOS 公共类-MyDateUtil 日期处理Util

    IOS 公共类-MyDateUtil 日期处理Util 此为处理日期的公共类.适用IOS6+ .h文件: #import <Foundation/Foundation.h> //适用 IO ...

  3. NSCalenda日历类

    1. //将数据库时间和当前时间相比,得出时间差. + (NSString *)dateDescriptionWithDate:(NSDate *)date{ // NSCalendar日历类,提供了 ...

  4. IOS 公共类-MyMBProgressUtil Progress显示

    IOS 公共类-MyMBProgressUtil Progress显示 此公共类用于显示提示框,对MBProgress的进一步封装.可以看下面的代码 接口: @interface MyMBProgre ...

  5. 一位iOS教育类应用开发者是如何赚到60多万美元?

    注:伯乐在线12月19日在@程序员的那些事 微博推荐了此文的英文原文,非常感谢@dotSlash 的翻译.  转眼距我写<我如何在iOS教育类应用中赚到20万美元>这篇博文已经一年多了,它 ...

  6. java学习笔记之日期日历类

    java学习笔记之日期日历 Date日期类概述: 表示特定的瞬间,精确到毫秒 Date类的构造方法: 1.空参数构造方法 Date date = new Date(); 获取到当前操作系统中的时间和日 ...

  7. 日历类和日期类转换 并发修改异常 泛型的好处 *各种排序 成员和局部变量 接口和抽象类 多态 new对象内存中的变化

    day07 ==和equals的区别? ==用于比较两个数值 或者地址值是否相同.  equals 用于比较两个对象的内容是否相同   String,StringBuffer.StringBuilde ...

  8. java基础22 日期类、日历类、日期格式类

    package com.dhb.code; import java.text.ParseException; import java.text.SimpleDateFormat; import jav ...

  9. PHP设计日历类一 (38)

    由两个文件组成: 第一个test.php <style> table { border:1px solid #; } .fontb { color:white; background:bl ...

随机推荐

  1. Hibernate - list()和iterate()的区别

    list()和iterate()都可以用来获得Query取得的HQL结果list()使用的是即时加载.查询时会之前去数据库查询HQL并将所有结果存在缓存中.iterate()使用的是延时加载.查询时只 ...

  2. C语言---字符

    1.三元符(三字母词):由三个字符组合起来代表其他字符,三元符可以在没有一些字符时使用 ??( [ ??) ] ??! | ??< { ??> } ??' ^ ??= # ??/ \ ?? ...

  3. li 水平排列并自动填满 ul

    找了li 如何水平排列并自动填满 ul,同时 li 宽度平均?资料,里面有提到"请用js动态计算保证兼容性", 因为我想实现的是,水平滚动条,ul的上级div是固定的宽度1000p ...

  4. webbroswer 后台注入脚本 的方法

    HtmlElement script = webBrowser.Document.CreateElement("script"); script.SetAttribute(&quo ...

  5. js动态生成按钮,页面用DIV简单布局

    今天朋友让我忙帮给写个页面,由于时间紧破,所以没有完善,暂时先贴出来,以后有时间了在做修改 <!DOCTYPE html><html><head><title ...

  6. GitHub 中国区前100 名技术专家

    [本文是在一片新闻上摘录的,原地址为:http://mt.sohu.com/20160407/n443539407.shtml] 本文根据Github公开API,抓取了地址显示China的用户,根据粉 ...

  7. JavaScript入门(10)

    一.Window对象 window对象是BOM的核心,window对象指当前的浏览器窗口 window对象方法 二.JavaScript计时器 在JavaScript中,我们可以在设定的时间间隔之后来 ...

  8. Android寒假实训云笔记总结——欢迎页

    欢迎页使用的是viewpager,需要适配器. 注意点: 1.判断是否是第一次进入这个app. 2.欢迎页小圆点的逻辑. 实现原理: 首先在activity_welcome放入viewpager和固定 ...

  9. linux shell if参数

    shell 编程中使用到得if语句内判断参数 –b 当file存在并且是块文件时返回真 -c 当file存在并且是字符文件时返回真 -d 当pathname存在并且是一个目录时返回真 -e 当path ...

  10. 安装oracle 12c遇到问题

    安装前步骤:更改用户账户控制设置:从不通知  出现 "SEVERE: [FATAL] [INS-30014] 无法检查指定的位置是否位于 CFS 上" 解决办法:重新设置hosts ...