我正在以下列格式获取数据
20130224233000 where,
2013 - year
02 month
24 day
23 hh
30 mm
00 ss
我希望采用以下格式
Wednesday, 24-Feb-13 11:30 PM
我尝试了以下代码
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy hh:mma"];
NSString *dateString = [dateFormat stringFromDate:today];
return dateString;
但它提供了一种我不需要的格式
答案 0 :(得分:3)
您需要两个NSDateFormatters,一个转换为NSDate,另一个转换为NSString。使用format table作为参考。
NSString* date = @"20130224233000";
NSDateFormatter *parser = [[NSDateFormatter alloc] init];
[parser setDateFormat:@"yyyyMMddHHmmss"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"eeee, dd-MMM-yy hh:mm a"];
NSString *dateString = [formatter stringFromDate:[parser dateFromString:date]];
return dateString;
答案 1 :(得分:0)
试试这个:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEE d-MMM-yy HH:mma"];
NSString *dateString = [dateFormat stringFromDate:today];
return dateString;
答案 2 :(得分:0)
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[dateFormat setDateFormat:@"EEEE, dd-MMM-yy HH:mm a"];
NSString *dateString = [dateFormat stringFromDate:today];
return dateString;
希望这有帮助。