由 NSDate 转换为 NSString:
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
- NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
- NSLog(@"%@", strDate);
- [dateFormatter release];
结果:
- 2010-08-04 16:01:03
由 NSString 转换为 NSDate:
- 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];
结果:
- 2010-08-04 16:01:03 +0800
日期之间比较可用以下方法