ios4 - 将字符串中的日期和时间分成两个单独的字符串
我的应用程序中有一个JSON提要,其中一个字段是组合日期&时间字符串,我需要拆分成离散的日期和时间字符串,以便在表格单元格中显示。来自JSON的输入示例是:
I have a JSON feed coming into my app, one of the fields is a combined date & time string which I need to split into discrete date and time strings for display in a table cell. An example of input from the JSON is:
2012-01-18 14:18:00.
我对日期格式化程序感到有些困惑,显然我做得不对 - 我已经尝试了很多教程,但大多数似乎只是展示了如何设置日期格式。
I'm getting a bit confused with the date formatter, and clearly I'm not doing it right - I've tried a number of tutorials but most just seem to show how to format a date.
我尝试了一些像这样的东西来获得时间:
I've tried something a little like this to get just the time:
NSDictionary *rowData = [self.raceData objectAtIndex:[indexPath row]];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@"h:mma"];
NSDate *raceDate = [dateFormat dateFromString:[rowData valueForKey:@"race_time"]];
NSString *raceTime = [dateFormat stringFromDate:raceDate];
但输出时raceTime只是空。
but on output raceTime is just null.
任何帮助表示赞赏。
如果您确定输入字符串格式不会改变 - 您可能会使用类似的东西至:
If you are sure that the input string format wouldn't change – you might use something similar to:
NSString *date = nil;
NSString *time = nil;
NSDictionary *rowData = [self.raceData objectAtIndex:[indexPath row]];
NSString *raceTime = [rowData valueForKey:@"race_time"];
NSArray *dateParts = [raceTime componentsSeparatedByString:@" "];
if ([dateParts count] == 2) {
date = [dateParts objectAtIndex:0];
time = [dateParts objectAtIndex:1];
}