IOS NSDate
来源:互联网 发布:linux服务器禁用ip 编辑:程序博客网 时间:2024/05/18 00:29
NSDate
- NSDate对象用来表示一个具体的时间点。
- NSDate是一个类簇,我们所使用的NSDate对象,都是NSDate的私有子类的实体。
- NSDate存储的是GMT时间,使用的时候会根据 当前应用 指定的 时区 进行时间上的增减,以供计算或显示。
可以快速地获取的时间点有:
now (当前时间点)相对于1 January 2001, GMT的时间点相对于1970的时间点distantFuture (不可达到的未来的某个时间点)distantPast (不可达到的过去的某个时间点
根据http://www.gnustep.org/实现的NSDate的版本:
<!-- lang: cpp -->@interface NSDate : NSObject <NSCoding, NSCopying>{ NSTimeInterval _secondsSinceRef;}……- (id) initWithTimeInterval:(NSTimeInterval) secsToBeAdded sinceDate:(NSDate *) anotherDate; 相对于已知的某个时间点- (id) initWithTimeIntervalSinceNow:(NSTimeInterval) secsToBeAdded; 相对于当前时间- (id) initWithTimeIntervalSince1970:(NSTimeInterval)seconds; 相对于1970年1月1日0时0分0秒- (id) initWithTimeIntervalSinceReferenceDate:(NSTimeInterval) secs; 相对于2001年1月1日0时0分0秒……@end
可以看出,NSDate类确实只是一个相对的时间点,NSTimeInterval的单位是秒(s),_secondsSinceRef则说明NSDate对象是相对于ReferenceDate(2001年1月1日0时0分0秒)的一个时间点。
同时,根据Cocoa框架的设计原则,每个类都有一个“指定初始化方法”(指定初始化方法是参数最全,且其他初始化方法都会调用的初始化方法)。http://www.gnustep.org/实现的版本以方法:
<!-- lang: cpp -->- (id) initWithTimeIntervalSinceReferenceDate:(NSTimeInterval) secs;
作为指定初始化方法,也就是说所有的时间点都转化为了相对referenceDate的时间点(时间点都是相对的,因为时间本身就是相对的)。
NSDate中最常用的方法一般是:
<!-- lang: cpp -->NSDate *now = [NSDate date]; // [[NSDate alloc] init]NSDate *dateFromNow = [NSDate dateWithTimeIntervalSinceNow:60];NSDate *dateFromAnotherDate = [[NSDate alloc] initWithTimeInterval:60 sinceDate:dateFromNow];NSTimeInterval timeInterval1 = [now timeIntervalSinceDate:dateFromNow];NSTimeInterval timeInterval2 = [now timeIntervalSinceNow];//-------------------------------------------------------------NSDate *distantPast = [NSDate distantPast]; // 可以表示的最早的时间NSDate *distantFuture = [NSDate distantFuture]; // 可以表示的最远的未来时间NSString *stringDate = @"12/31/9999";NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];[dateFormatter setDateFormat:@"MM/dd/yyyy"];NSDate *dateCheck = [dateFormatter dateFromString:stringDate];NSLog(@"Date = %@", dateCheck);Output:Date = 1999-12-30 16:00:00 +0000*iOS中用NSDate表示的时间只能在distantPast和distantFuture之间!
0 0
- iOS NSDate
- ios NSDate
- iOS NSDate
- iOS NSDate
- IOS NSDate
- iOS NSDate
- IOS NSDate
- iOS NSDate
- iOS nsdate
- ios NSDate
- iOS NSDate
- IOS NSDate
- ios 操作日期NSDate
- iOS NSDate日期加减
- iOS NSDate日期加减
- ios--NSDate格式化(随笔)
- iOS NSTimeInterval 转成 NSDate
- iOS NSTimeInterval 转成 NSDate
- MySQL查看数据库连接数和状态
- sql中的group by 和 having 用法解析
- Dagger 2 入门
- 线索二叉树
- Algs4-1.4.22斐波那契查找算法(Fibonacci Search)Java 实现
- IOS NSDate
- ios NSString format 保留小数点 float double
- 【MFC】如何使编辑框自动滚动到最后一行,就像QQ那样?
- 初学必看的lvs负载均衡教程
- java中collection、map、set、list简介
- 遇见C++ Lambda
- Dell服务器BIOS操作
- MPMoviePlayer浅谈
- mysql基础知识