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秒的时间
  • + (id)distantFuture //返回很多年以后的未来的某一天(随机)
  • + (id)distantPast //返回很多年以前的某一天(随机)

NSDate的动态初始化

  • - (id)addTimeInterval:(NSTimeInterval)secs //返回以目前实例中保存的时间为基准,然后过了secs秒的时间
  • - (id)init //初始化为当前时间。[NSDate date]
  • - (id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs //返回以2001/01/01 GMT为基准,然后过了secs秒的时间
  • - (id)initWithTimeInterval:(NSTimeInterval)secs sinceDate:(NSDate *)refDate //初始化为以refDate为基准,然后过了secs秒的时间
  • - (id)initWithTimeIntervalSinceNow:(NSTimeInterval)secs //初始化为以当前时间为基准,然后过了secs秒的时间

NSDate日期比较

  • - (BOOL)isEqualToDate:(NSDate *)otherDate //与otherDate比较,相同返回YES
  • - (NSDate *)earlierDate:(NSDate *)anotherDate //与anotherDate比较,返回较早的那个日期
  • - (NSDate *)laterDate:(NSDate *)anotherDate //与anotherDate比较,返回较晚的那个日期
  • - (NSComparisonResult)compare:(NSDate *)other 

compare用于排序时调用,相同,返回NSOrderedSame,大于other返回NSOrderedDescending,小于other返回NSOrderedAscending

NSDate取回时间间隔

  • - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate //以refDate为基准时间,返回实例保存的时间与refDate的时间间隔
  • - (NSTimeInterval)timeIntervalSinceNow //以当前时间(Now)为基准时间,返回实例保存的时间与当前时间(Now)的时间间隔
  • - (NSTimeInterval)timeIntervalSince1970 //以1970/01/01 GMT为基准时间,返回实例保存的时间与1970/01/01 GMT的时间间隔
  • - (NSTimeInterval)timeIntervalSinceReferencedate //以2001/01/01 GMT为基准时间,返回实例保存的时间与2001/01/01 GMT的时间间隔
  • + (NSTimeInterval)timeIntervalSinceReferenceDate //以2001/01/01 GMT为基准时间,返回当前时间(Now)与2001/01/01 GMT的时间间隔

NSDate日期格式化

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];

formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";

//将NSString转换为

NSDate NSDate *date = [formatter dateFromString:@"2001-03-24 00:00:00"];

//将NSDate转换为NSString, 返回GMT时间

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

 

posted on 2014-07-25 23:05  Martin Hu  阅读(127)  评论(0编辑  收藏  举报

导航