我的日期类似于"payment_date" = "03:12:00 May 06, 2014 MDT";
但我想将此数据转换为NSDate
,如"06-05-2014"
请帮帮我, 不知道处理它..
我从Converting NSString to NSDate (and back again)
得到答案 __block NSDate *detectedDate;
//Detect.
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingAllTypes error:nil];
[detector enumerateMatchesInString:t_date
options:kNilOptions
range:NSMakeRange(0, [t_date length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{ detectedDate = result.date;
NSDate *d=result.date;
}
];
答案 0 :(得分:-2)
EDITS
NSString *str = @"03:12:00 October 06, 2014 MDT";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
[df setDateStyle:NSDateFormatterLongStyle];
str = [str substringFromIndex:9];
NSDate *date = [df dateFromString:[str substringToIndex:[str length] -3]];
[df setDateFormat:@"dd-MM-YYYY"];
NSLog(@"%@",[df stringFromDate:date]);