創(chuàng)建時(shí)間:2017-04-17
一妻熊、NSDate、NSCalendar和NSDateComponents介紹
NSDate
1.NSDate對(duì)象用來表示一個(gè)具體的時(shí)間點(diǎn)趁桃;
2.NSDate是一個(gè)類簇辽话。我們所使用的NSDate對(duì)象,都是NSDate的私有之類的實(shí)體卫病;
3.NSDate儲(chǔ)存的是GMT時(shí)間油啤,使用的時(shí)候會(huì)根據(jù)當(dāng)前應(yīng)用指定的時(shí)區(qū)進(jìn)行時(shí)間上的增減,以供計(jì)算或顯示蟀苛。
類方法初始化
//返回當(dāng)前時(shí)間(獲取的是GMT時(shí)間,需要獲取某個(gè)時(shí)區(qū)的時(shí)間益咬,看常用方法)
+ (instancetype)date;
//返回以當(dāng)前時(shí)間為基準(zhǔn),然后過了secs秒的時(shí)間
+ (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;
//返回以2001-01-01 GMT為基準(zhǔn)帜平,然后過了secs秒的時(shí)間
+ (instancetype)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti;
//返回以1970-01-01 GMT為基準(zhǔn)幽告,然后過了secs秒的時(shí)間
+ (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;
//返回以date時(shí)間為基準(zhǔn),過了secsToBeAdded秒的時(shí)間
+ (instancetype)dateWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;
實(shí)例方法初始化
//初始化當(dāng)前時(shí)間(獲取的是GMT時(shí)間,需要獲取某個(gè)時(shí)區(qū)的時(shí)間裆甩,看常用方法)
- (instancetype)init;
//初始化以當(dāng)前時(shí)間為基準(zhǔn)冗锁,然后過了secs秒的時(shí)間
- (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;
//初始化以1970-01-01 GMT為基準(zhǔn),然后過了secs秒的時(shí)間
- (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs;
//初始化以date時(shí)間為基準(zhǔn)嗤栓,過了secsToBeAdded秒的時(shí)間
- (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;
時(shí)間之間的比較
//與anotherDate比較冻河,返回較早的那個(gè)日期
- (NSDate *)earlierDate:(NSDate *)anotherDate;
//與anotherDate比較,返回較晚的那個(gè)日期
- (NSDate *)laterDate:(NSDate *)anotherDate;
/*該方法用于排序時(shí)調(diào)用:
當(dāng)實(shí)例保存的日期值與anotherDate相同時(shí)返回NSOrderedSame
當(dāng)實(shí)例保存的日期值晚于anotherDate時(shí)返回NSOrderedDescending
當(dāng)實(shí)例保存的日期值早于anotherDate時(shí)返回NSOrderedAscending */
- (NSComparisonResult)compare:(NSDate *)other;
//與otherDate比較茉帅,相同返回YES
- (BOOL)isEqualToDate:(NSDate *)otherDate;
取回時(shí)間間隔
//以refDate為基準(zhǔn)時(shí)間芋绸,返回實(shí)例保存的時(shí)間與refDate的時(shí)間間隔
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate;
//以當(dāng)前時(shí)間(Now)為基準(zhǔn)時(shí)間,返回實(shí)例保存的時(shí)間與當(dāng)前時(shí)間(Now)的時(shí)間間隔
- (NSTimeInterval)timeIntervalSinceNow;
//以1970/01/01 GMT為基準(zhǔn)時(shí)間担敌,返回實(shí)例保存的時(shí)間與1970/01/01 GMT的時(shí)間間隔
- (NSTimeInterval)timeIntervalSince1970;
//以2001/01/01 GMT為基準(zhǔn)時(shí)間,返回實(shí)例保存的時(shí)間與2001/01/01 GMT的時(shí)間間隔
- (NSTimeInterval)timeIntervalSinceReferenceDate;
+ (NSTimeInterval)timeIntervalSinceReferenceDate;
相關(guān)用法
獲取當(dāng)前時(shí)間
- (NSDate *)getLocalDate {
NSDate *date = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:date];
NSDate *localDate = [date dateByAddingTimeInterval:interval];
return localDate;
}
將時(shí)間表示為字符串
//以YYYY-MM-DD HH:MM:SS ±HHMM的格式表示時(shí)間廷蓉。其中 "±HHMM" 表示與GMT的存在多少小時(shí)多少分鐘的時(shí)區(qū)差異全封。比如,若時(shí)區(qū)設(shè)置在北京桃犬,則 "±HHMM" 顯示為 "+0800"
- (NSString *)description;
NSCalendar
NSCalendar:日歷刹悴。對(duì)世界上現(xiàn)存的常用歷法進(jìn)行了封裝,即提供了不同的歷法的時(shí)間信息攒暇,又支持日歷的計(jì)算土匀。
NSDateComponents
NSDateComponents:時(shí)間容器,一個(gè)包含了詳細(xì)的年月日時(shí)分秒的容器形用。
常用方法合集
拆分時(shí)間
<pre><code>
NSDate *currentDate = [NSDate date];//當(dāng)前時(shí)間
NSCalendar *calendar = [NSCalendar currentCalendar];//當(dāng)前用戶的calendar
NSDateComponents * components = [calendar components:NSCalendarUnitYear | NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitMonth | NSCalendarUnitHour | NSCalendarUnitDay fromDate:currentDate];
NSLog(@"%ld年%ld月%ld日%ld時(shí)%ld分%ld秒",(long)components.year ,(long)components.month,(long)components.day,(long)components.hour,(long)components.minute,(long)components.second);
</pre></code>
查看今天是今年的第幾周
<pre><code>
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *currentDate = [NSDate date];
NSInteger week = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitYear forDate:currentDate];
NSLog(@"今天是今年的第%ld周",week);
</pre></code>
查看今天是當(dāng)月的第幾周(舉一反三就轧,和上一個(gè)方法一樣证杭,改變參數(shù)即可)
<pre><code>
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *currentDate = [NSDate date];
NSInteger week = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitMonth forDate:currentDate];
NSLog(@"今天是當(dāng)月的第%ld周",week);
</pre></code>
根據(jù)拆封時(shí)間返回NSDate
<pre><code>
NSDateComponents * components = [[NSDateComponents alloc] init];
components.year = 2015;
components.month = 9;
components.day = 28;
components.hour = 14;
components.minute = 38;
components.second = 20;
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDate * date = [calendar dateFromComponents:components];
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy年MM月dd日hh時(shí)mm分ss秒";
NSString * str = [formatter stringFromDate:date];
NSLog(@"%@",str);
</pre></code>
相對(duì)時(shí)間(從現(xiàn)在往后2年5個(gè)月11天10小時(shí),年月日時(shí)分秒均可配置)
<pre><code>
NSDateComponents * components = [[NSDateComponents alloc] init];
components.year = 2;
components.month = 5;
components.day = 11;
components.hour = 10;
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDate * currentDate = [NSDate date];
NSDate * nextData = [calendar dateByAddingComponents:components toDate:currentDate options:NSCalendarMatchStrictly];
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy年MM月dd日hh時(shí)mm分ss秒";
NSString * str = [formatter stringFromDate:nextData];
NSLog(@"%@",str);
</pre></code>
獲取當(dāng)月的天數(shù)
//1.1 計(jì)算當(dāng)月的天數(shù)
- (NSInteger)getNumberOfDaysInMonth {
// NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];//指定日歷的算法
NSDate *currentDate = [NSDate date];//當(dāng)前時(shí)間
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:currentDate];
return range.length;
}
獲取指定時(shí)間當(dāng)月的天數(shù)
/**
1.2 計(jì)算某個(gè)時(shí)間的當(dāng)月天數(shù)
@param targetDate 目標(biāo)時(shí)間
@return 天數(shù)
*/
- (NSInteger)getNumberOfDaysInMonth:(NSDate *)targetDate {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];//指定日歷的算法
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:targetDate];
return range.length;
}
獲取指定日期的年妒御、月解愤、日、星期乎莉、時(shí)送讲、分、秒
/**
1.3 獲取指定日期的年惋啃、月哼鬓、日、星期边灭、時(shí)异希、分、秒
@param targetDate 目標(biāo)時(shí)間
*/
- (void)getDateInfo:(NSDate *)targetDate {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];//指定日歷的算法
NSDateComponents *comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekday fromDate:targetDate];//NSDateComponents可以獲取日期的詳細(xì)信息存筏,所有的信息獲取是可配置的
NSLog(@"年 = year = %ld",comps.year);
NSLog(@"月 = month = %ld",comps.month);
NSLog(@"日 = day = %ld",comps.day);
NSLog(@"時(shí) = hour = %ld",comps.hour);
NSLog(@"分 = minute = %ld",comps.minute);
NSLog(@"秒 = second = %ld",comps.second);
NSLog(@"周 = weekDay = %ld ",comps.weekday);
}
字符串轉(zhuǎn)化為時(shí)間
NSString *dateStr = @"2017-4-17 13:38:00";
_gqTimer = [[GQTimer alloc] init];
NSDate *date = [_gqTimer strToDate:dateStr];
/**
2.1 字符串轉(zhuǎn)化為時(shí)間
@param targetStr 目標(biāo)字符串
@return 轉(zhuǎn)化得到的時(shí)間
*/
- (NSDate *)strToDate:(NSString *)targetStr {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//年-月-日 時(shí):分:秒(這里千萬注意大小寫宠互。yyyy年、MM月椭坚、dd日予跌、HH小時(shí)、mm分鐘善茎、ss秒券册,格式和順序隨便定義,如:yyyy年MM月dd日HH時(shí)mm分ss秒)
NSDate *date = [formatter dateFromString:targetStr];
return date;
}
時(shí)間轉(zhuǎn)化為字符串
/**
2.2 時(shí)間轉(zhuǎn)換成字符串
@param targetDate 目標(biāo)時(shí)間
@return 轉(zhuǎn)化得到的字符串
*/
- (NSString *)dateToStr:(NSDate *)targetDate {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//可以根據(jù)自己的需要設(shè)置格式(如@"MM垂涯,dd")
NSString *dateStr = [formatter stringFromDate:targetDate];
return dateStr;
}
獲取兩個(gè)時(shí)間的天數(shù)差
/**
3.1 獲取兩個(gè)時(shí)間的天數(shù)差
@param firstDate 第一個(gè)時(shí)間
@param secondDate 第二個(gè)時(shí)間
@return 比較得出的天數(shù)差
*/
- (NSInteger)getDateToDateDays:(NSDate *)firstDate withSaveDate:(NSDate *)secondDate {
NSCalendar* chineseClendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMinute |
NSCalendarUnitSecond | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
NSDateComponents *cps = [chineseClendar components:unitFlags fromDate:firstDate toDate:secondDate options:0];
NSInteger diffDay = [cps day];
return diffDay;
}