ios4 - 将字符串中的日期和时间分成两个单独的字符串 [英] ios4 - splitting a date and time from a string into two separate strings

查看:93
本文介绍了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];
}

这篇关于ios4 - 将字符串中的日期和时间分成两个单独的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆