准备:
DateTools是Objective-C中简化日期和时间处理的工具.
安装
CocoaPodspod 'DateTools'
手动安装
DataTools所需的所有类包含在DataTools文件夹下,如下:
DateTools.h
NSDate+DateTools.{h,m}
DTConstants.h
DTError.{h,m}
DTTimePeriod.{h,m}
DTTimePeriodGroup.{h,m}
DTTimePeriodCollection.{h,m}
DTTimePeriodChain.{h,m}
如果你的工程想要支持国际化或者使用”TimeAgo”功能必须要导入bundle文件
你可以在工程中Info下添加Localizations来添加支持的语言
DateTools.bundle
DateTools.h
包含了所有其他文件的头文件.在你的工程中导入DateTools.h头文件即可
DateTools中包含3个子库
- NSDate+DateTools
- Time Periods
- Time Period Groups
基本使用
NSDate+DateTools
1.Time Ago(相对日期形式)
Time ago 就是将日期转变为相对日期的形式,即我们常用的“昨天、今天、明天、几天前,一周以后……”这样的表述方式。
1 | NSDate *timeAgoDate = [NSDate dateWithTimeIntervalSinceNow:-4]; |
DateTools提供了多达33种语言的支持
如果想修改相对日期显示的文字,可以修改DateTools/Resources/DateTools.bundle下相应的文件
下图以简体中文为例:
2.Date Components(日期组成部分)
获取日期的组成部分:
1 | NSDate *date = [NSDate date]; |
DateTools默认日历为公历!!!
使用其他日历:
(如果想全局使用其他日历,修改DateTools默认日历,可以改写NSDate+DateTools.m
中的defaultCalendar
方法)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17/**
* 使用中国农历
*/
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];
NSDate *date = [NSDate date];
NSInteger year = [date yearWithCalendar:calendar];
NSInteger month = [date monthWithCalendar:calendar];
NSInteger day = [date dayWithCalendar:calendar];
NSInteger hour = [date hourWithCalendar:calendar];
NSInteger minute = [date minuteWithCalendar:calendar];
NSInteger second = [date secondWithCalendar:calendar];
NSLog(@"农历: %ld-%ld-%ld %ld:%ld:%ld", year, month, day, hour, minute, second);
NSLog(@"公历: %ld-%ld-%ld %ld:%ld:%ld", date.year, date.month, date.day, date.hour, date.minute, date.second);
//输出: 农历: 33-4-17 11:41:51
//输出: 公历: 2016-5-23 11:41:51
3.日期编辑
对日期进行编辑(年,月,日,星期,时,分,秒)
增加/减少方法:
1
2
3
4
5
6
7
8/**
* 日期增加1年
*/
NSDate *date = [NSDate date];
NSDate *newDate = [date dateByAddingYears:1];
NSLog(@"year:%ld newYear:%ld", date.year, newDate.year);
//输出: year:2016 newYear:2017
4.日期比较
比较两个日期大小返回一个布尔值:
更多日期比较方法:
(很容易的得到两个日期相差的年,月,星期,日,时,分,秒)
5.格式化日期字符串
formattedDateWithStyle:(系统格式)
formattedDateWithFormat:(自定义格式)
1 | /** |
字符 | 说明 |
---|---|
yy | 年的后2位 |
yyyy | 完整年 |
M | 1~12 月(不带0) |
MM | 1~12 月(带0) |
MMM | Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec (1~12月_简写) |
MMMM | January/February/March/April/May/June/July/August/September/October/November/December (1~12月_全写) |
d | 1~31 日(不带0) |
dd | 1~31 日(带0) |
EEE | Sun/Mon/Tue/Wed/Thu/Fri/Sat (星期_简写) |
EEEE | Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday (星期_全写) |
aa | AM/PM (上午/下午) |
H | 0~23 时(24小时制,不带0) |
HH | 0~23 时(24小时制,带0) |
h | 1~12 时(12小时制,不带0) |
hh | 1~12 时(24小时制,带0) |
m | 0~59 分(不带0) |
mm | 0~59 分(带0) |
s | 0~59 秒(不带0) |
ss | 0~59 秒(带0) |
S | 毫秒 |
Time Periods「时间段」
DateTools提供DTTimePeriod来简化时间段的操作
初始化和获取时间段信息
1 | /** |
更多初始化时间段方法:
时间段操作
移动时间段:
shiftEarlierWithSize 时间段整体前移
shiftLaterWithSize 时间段整体后移
1 | /** |
延长/缩短时间段:
shortenWithAnchorDate 延长(开始时间/中间时间/结束时间)
lengthenWithAnchorDate 缩短(开始时间/中间时间/结束时间)
1 | /** |
时间段关系
两个时间段关系相关方法:
两个时间段关系所有可能性:
时间段关系枚举DTTimePeriodRelation:
1 | /** |
#Time Period Groups「时间段组」
DateTools提供两种时间段组类:
DTTimePeriodCollection 时间段集合 (允许存储彼此有交集的时间段)
DTTimePeriodChain 时间段链接 (不允许存储彼此有交集的时间段)DTTimePeriodCollection和DTTimePeriodChain就像NSArray一样,你可以像操作数组一样对它们中的DTTimePeriod对象进行添加,插入,删除,不同之处在于如何处理时间段
DTTimePeriodCollection (时间段集合)
一个规则相对宽松的时间段集合
默认是无序的,但是支持通过调用方法排序
有自己的属性,例如StartDate和EndDate属性是根据内部时间段推测出来的
允许存储有重叠的时间段
1 | //时间段集合 |
获取一个NSDate对象或一个DTTimePeriod对象与一个时间段集合的相对关系
DTTimePeriodChain (时间链)
一个紧密耦合的时间段集合
通常依据开始时间和结束时间存储时间段对象
有自己的属性,例如StartDate和EndDate属性是根据内部时间段推测出来的
不允许存储有重叠的时间段
1 |
|
新加入的时间段,时长不变,起始时间变为前一个时间段的结束时间,结束时间对应前移后后移.在非零位置新插入的时间,其后的时间段相应后移.在零位置插入的时间,集合的起始时间前移