- (NSString *)exchangeCreatedToDate:(NSString *)created {
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateTime = [formatter dateFromString:created];
NSCalendar *myCal = [[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
unsigned units = NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit;
NSDateComponents *comp = [myCal components:units fromDate:dateTime];
NSInteger month = [comp month];
NSInteger day = [comp day];
NSMutableString *date = [[NSMutableString alloc] init];
if (month < 10) {
[date appendString:@"0"];
}
[date appendFormat:@"%d-", month];
if (day < 10) {
[date appendString:@"0"];
}
[date appendFormat:@"%d", day];
return [date autorelease];
}
注意:
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
这个日期格式,需要根据自己要转化的格式来决定;
结果就是:2012-07-31 => 07-31