NSString * dateStr = [dic objectForKey: @ "date"]; // 2012-05-17 11:23:23
NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateFormatter * format = [[NSDateFormatter alloc] init];
[Format setDateFormat: @ "yyyy-MM-dd HH: mm: ss"];
NSDate * fromdate = [format dateFromString: dateStr];
NSTimeZone * fromzone = [NSTimeZone systemTimeZone];
NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate];
NSDate * fromDate = [fromdate dateByAddingTimeInterval: frominterval];
NSLog (@ "fromdate = % @", fromDate );
[Format release];
NSDate * date = [NSDate date];
NSTimeZone * zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate * localeDate = [date dateByAddingTimeInterval: interval];
NSLog (@ "enddate = % @", localeDate );
NSDateComponents * components = [gregorian components: unitFlags fromDate: fromDate toDate: localeDate options: 0];
NSInteger months = [components month];
NSInteger days = [components day]; // year [components year]
NSLog (@ "month = % d", months );
NSLog (@ "days = % d", days );
[Gregorian release];
If (months = 0 & days = 0 ){
DateStr = [[dateStr substringFromIndex: 11] substringToIndex: 5];
Cell. textLabel. text = [NSString stringWithFormat: @ "today % @", dateStr]; // Today
} Else if (months = 0 & days = 1 ){
DateStr = [[dateStr substringFromIndex: 11] substringToIndex: 5];
Cell. textLabel. text = [NSString stringWithFormat: @ "Yesterday % @", dateStr]; // Yesterday
} Else {
DateStr = [dateStr substringToIndex: 10];
Cell. textLabel. text = dateStr;
}
From shijiucdy's column