objective-C之NSDate相关类(一)

一,概述
    NSDate是专门处理时间对象的一个类簇,我们所使用的NSDate对象,都是NSDate的私有子类的实体. NSDate表示一个具体的时间点,可以进行一些常见的日期\时间处理.
二,常用初始化方法
1>+ (instancetype)date
  示例

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. NSDate * date =[NSDate date];// 创建时间对象,获得当前时间  

 注意:date方法返回的是当前时间(now), 但是这个时间是格林尼治时间(GMT),并不是我们传统意义上的北京时间. 格林威治时间属于0时区,北京属于东8区;

 比如: 北京时间是上午11:04

 打印出来的结果确是

2>- (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date

 示例   

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. NSDate * date = [[NSDate alloc] initWithTimeInterval:60 sinceDate:[NSDate date]];  

含义: 获取从某个日期开始往前或者往后多久的日期,此处60代表60秒,如果需要获取之前的,将60改为-60即可

注意:NSTimeInterval   是双精度类型,单位是秒.    

typedefdouble NSTimeInterval;

三,其它相关方法和属性

typedef double NSTimeInterval;
#define NSTimeIntervalSince1970  978307200.0

1>NSDate类

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @interface NSDate : NSObject <NSCopying, NSSecureCoding>  
  2. @property (readonly) NSTimeInterval timeIntervalSinceReferenceDate;  
  3. - (instancetype)init NS_DESIGNATED_INITIALIZER;  
  4. - (instancetype)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti NS_DESIGNATED_INITIALIZER;  
  5. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;  
  6. @end  

2>NSDate分类拓展方法_NSDate (NSExtendedDate)

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @interface NSDate (NSExtendedDate)  
  2. - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate; //获取两个时间的时间差  
  3. @property (readonly) NSTimeInterval timeIntervalSinceNow;  
  4. @property (readonly) NSTimeInterval timeIntervalSince1970// 返回1970-1-1开始走过的毫秒数  
  5. - (id)addTimeInterval:(NSTimeInterval)secs;//返回以目前的实例中保存的时间为基准,然后过了secs秒的时间  
  6. - (instancetype)dateByAddingTimeInterval:(NSTimeInterval)ti;  
  7.   
  8. - (NSDate *)earlierDate:(NSDate *)anotherDate; // 返回比较早的那个时间   
  9. - (NSDate *)laterDate:(NSDate *)anotherDate;     // 返回比较晚的那个时间   
  10. - (NSComparisonResult)compare:(NSDate *)other;  
  11.     该方法用于排序时调用:
           . 当实例保存的日期值与anotherDate相同时返回NSOrderedSame
           . 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending
           . 当实例保存的日期值早于anotherDate时返回NSOrderedAscending
  12. - (BOOL)isEqualToDate:(NSDate *)otherDate;//与otherDate比较,相同返回YES
  13.   
  14. @property (readonlycopyNSString *description;  
  15. - (NSString *)descriptionWithLocale:(nullable id)locale;  
  16. + (NSTimeInterval)timeIntervalSinceReferenceDate;  
  17. @end  

3>NSDate分类拓展方法_NSDate (NSDateCreation)

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @interface NSDate (NSDateCreation)  
  2. /*类方法*/  
  3. + (instancetype)date; //当前时间  
  4. + (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;//相对于当前时间  
  5. + (instancetype)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti; //相对于2001年1月1日0时0分0秒  
  6. + (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;//相对于1970年1月1日0时0分0秒  
  7. + (instancetype)dateWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;  //相对于已知的某个时间点  
  8.   
  9. + (NSDate *)distantFuture; //不可达到的未来的某个时间点  
  10. + (NSDate *)distantPast;    //不可达到的过去的某个时间点  
  11. 注意: iOS中用NSDate表示的时间只能在distantPast和distantFuture之间!  
  12.   
  13. /*实例方法*/  
  14. - (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs; //相对于当前时间  
  15. - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs; //相对于1970年1月1日0时0分0秒  
  16. - (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date; //相对于已知的某个时间点  
  17. @end  

四,相关用法

1>显示当前时间

NSDate ->NSString

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. NSDate *date = [NSDate date];  
  2. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];  
  3. [dateFormatter setDateFormat:@"yyyy/MM/dd hh:mm:ss"];  
  4. self.timeLabel.text = [dateFormatter stringFromDate:date];  

NSString ->NSDate

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. NSString *dateString = self.timeLabel.text;  
  2. NSDate *nowDate = [dateFormatter dateFromString:dateString];  
  3. self.timeLabel2.text = [dateFormatter stringFromDate:nowDate];  

2>获取指定日期的年,月,日,时,分,秒

第一种写法:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];  
  2.   NSString *dateString  = @"2016/06/06 3:2:34";  
  3.   NSDate *theDate       = [dateFormatter dateFromString:dateString];  
  4.   NSDateComponents *com = [[NSCalendar currentCalendar]components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:theDate];          NSInteger year          = com.year;  
  5.  NSInteger month       = com.month;  
  6.  NSInteger day           = com.day;  
  7.  NSInteger hour          = com.hour;  
  8.  NSInteger minute      = com.minute;  
  9.  NSInteger second     = com.second;  
  10.  self.timeLabel2.text   = [NSString stringWithFormat:@"%ld年%ld月%ld日 %02ld时:%02ld分:%02ld秒, year, month, day, hour, minute, second];  

第二种写法:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. NSString *dateString     = @"2016/06/06 3:2:34";  
  2.   NSDate *theDate        = [dateFormatter dateFromString:dateString];     
  3.   NSCalendar *calendar   = [NSCalendar currentCalendar];  
  4.   NSUInteger unitFlags   = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute |NSCalendarUnitSecond;  
  5.   NSDateComponents *cmp1 = [calendar components:unitFlags fromDate:theDate];  
  6.   NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]];  
  7.   // 2.格式化日期  
  8.   NSDateFormatter *formatter = [[NSDateFormatter alloc] init];  
  9.   if ([cmp1 day] == [cmp2 day]) { // 今天  
  10.       formatter.dateFormat = @"今天 HH:mm";  
  11.   } else if ([cmp1 year] == [cmp2 year]) { // 今年  
  12.       formatter.dateFormat = @"MM-dd HH:mm";  
  13.    } else {  
  14.       formatter.dateFormat = @"yyyy-MM-dd HH:mm";  
  15.   }  
  16.   NSString *time = [formatter stringFromDate:theDate];  
  17.   // 3.显示时间  
  18.   self.timeLabel2.text = time;  

注意:通过改变NSDateFormatter的dateFormat表现形式可以实现各种你想要的时间表示形式,

比如

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. NSString *dateString  = @"2015/09/08 3:2:34";  
  2. NSDate *theDate       = [dateFormatter dateFromString:dateString];  
  3. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];  
  4. formatter.dateFormat = @"'日期:'yyyy有鬼MM  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值