标签:
NSDate *now = [NSDate date];
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
// @"yyyy-MM-dd HH:mm:ss EEE Z" 日期和 时区差 -> Mon +0800
// @"yyyy年MM月dd日 HH时mm分ss秒"
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *nowString = [fmt stringFromDate:now];
NSLog(@"%@", nowString);
#warning 字符串的格式必须和dateFormat格式一致
// @"2015年10月15日 12时20分32秒"
NSString *s = @"2013-1-15 12:20:32";
NSDate *lastDate = [fmt dateFromString:s];
NSLog(@"%@", lastDate);
NSString *lastS = [fmt stringFromDate:lastDate];
NSLog(@"%@", lastS);
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
// 时间差组件
NSDateComponents *components = [calendar components:unit fromDate:lastDate toDate:now options:NSCalendarWrapComponents];
NSLog(@"%ld %ld", components.year, components.month);
NSString *timeSpace;
if (components.year > 0) {
timeSpace = [NSString stringWithFormat:@"%ld年%ld月%ld天前", components.year, components.month, components.day];
} else {
if (components.month > 0) {
timeSpace = [NSString stringWithFormat:@"%ld月前", components.month];
} else {
if (components.day > 0) {
timeSpace = [NSString stringWithFormat:@"%ld天前", components.day];
}
}
}
NSLog(@"%@", timeSpace);
标签:
原文地址:http://www.cnblogs.com/vultures/p/4912147.html