NSDate

来源:互联网 发布:知乎 卢煜明 编辑:程序博客网 时间:2024/05/16 09:31

【NSDate】

【注】NSDate是时间类,可以创建一个时间点的对象。

【另】面向对象程序讲究一切事物皆对象,无论是具象事物还是抽象事物。


+ (id)date;
返回系统时间

+ (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs; 

返回以当前时间为基准,然后过了secs秒的时间

+ (id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)

secs;

返回以2001/01/01 GMT为基准,然后过了secs秒的时间

+ (id)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;

返回以1970/01/01 GMT为基准,然后过了secs秒的时间


- (NSTimeInterval)timeIntervalSinceNow;

以当前时间(Now)为基准时间,返回实例保存的时间与当前时间(Now)的时 间间隔

- (NSTimeInterval)timeIntervalSince1970;

以1970/01/01 GMT为基准时间,返回实例保存的时间与1970/01/01 GMT 的时间间隔

- (NSTimeInterval)timeIntervalSinceReferenceDate;

以2001/01/01 GMT为基准时间,返回实例保存的时间与2001/01/01 GMT 的时间间隔



+ (id)distantFuture;

返回很多年以后的未来的某⼀一天。 比如你需要⼀一个比现在(Now)()很长时间的时间值,则可以调用该方法。

+ (id)distantPast;

返回很多年以前的某⼀一天。

比如你需要⼀一个比现在(Now)()大很长时间的时间值,则可以调用该方法。


- (id)addTimeInterval:(NSTimeInterval)secs; 

返回以目前的实例中保存的时间为基准,然后过了secs秒的时间






【NSTimeZone】

【注】时区类,用于时区转换

+ (NSTimeZone *)systemTimeZone;

//返回当前系统时区

+ (NSDictionary *)abbreviationDictionary;

//返回TimeZone支持时区的键值对

+ (id)timeZoneWithAbbreviation:(NSString *)abbreviation;

//通过传入键值对的键,创建对应的时区对象。



【NSDateFormatter】

【注】时间戳,用于显示时间

//创建时间戳

NSDateFormatter * dateFormatter = [[NSDateFormatteralloc] init];


//使用时间戳的目的,是为了自定义时间的打印格式

dateFormatter.dateFormat =@"yyyyMMdd HH:mm:ss.S";

        

//后设置会顶替原设置

dateFormatter.dateFormat =@"yy-MM-dd ahhmmss";


//设置时区,如果不设,缺省是系统时区

dateFormatter.timeZone = [NSTimeZonetimeZoneWithAbbreviation:@"CST"];


//根据风格,返回时间打印的字符串

NSLog(@"%@", [dateFormatterstringFromDate:date]);



0 0
原创粉丝点击