--------------------------------------------------------------------------NSDate----------------------------------------------------------------------------
NSDate:用来获取系统运行时时间.
1:获取IOS当前的运行时间
这样获取的是:世界标准时(UTC) 比中国时间晚8个小时
NSDate *date = [NSDate date];
2:为NSDate增加减少时间
NSDate *date = [[NSDate alloc] init]; NSTimeInterval time = 5;//单位为秒 NSLog(@"%@",date); NSDate *newDate = [date dateByAddingTimeInterval:time];//加时间 //NSDate *newDate = [date dateByAddingTimeInterval:-time];//减时间 NSLog(@"%@",newDate);
注:遍历以往或者未来的天数可以通过 增加或减少时间来实现
3:比较时间是否相等
- (BOOL)isEqualToDate:(NSDate *)otherDate;//与otherDate比较,相同返回YES
4:与anotherDate比较,返回较早的那个日期
- (NSDate *)earlierDate:(NSDate *)anotherDate;
5:与anotherDate比较,返回较晚的那个日期
- (NSDate *)laterDate:(NSDate *)anotherDate;
6:计算两个时间的 时间间隔是多少 返回 秒值
[endTime timeIntervalSinceDate:beginTime];注: 如果返回结果 endTime > 0 说明 endTime大于beginTime 反之 小于0 小于beginTime
--------------------------------------------------------------------------NSDate----------------------------------------------------------------------------
-----------------------------------------------------------------NSDateFormatter------------------------------------------------------------------------
1:NSDate 转换成 NSString 格式的日期
NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *loctime = [formatter stringFromDate:date]; [formatter release]
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *date = [dateFormatter dateFromString:@"2010-08-04 16:01:03"]; NSLog(@"%@", date); [dateFormatter release];
-----------------------------------------------------------------NSDateFormatter------------------------------------------------------------------------
-----------------------------------------------------------------NSDateComponents------------------------------------------------------------------------
NSDateComponents 是通过 NSCalendar 实例化的
1:通过NSDate获取 NSDateComponents
NSDateComponents *pastDateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit fromDate:pastDate];
注:参数可以指定需要获取哪些格式的数据
-----------------------------------------------------------------NSDateComponents------------------------------------------------------------------------