我有一个字符串,其中有日期字段。我将字段作为NSString存储在我的数据库中,如2011-12-07 21:15:00 +0000。如何更改字符串的NSDateFormatter
我使用下面的代码,以获得从日期格式字符串的日期,如:
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss.S"];
我想在时间格式为hh一个上述日期格式,但它总是给空。 我的代码:
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss.S"];
NSDateFormatter *timeFormat = [[[NSDateFormatter alloc] init] autorelease];
[timeFormat setDateFormat:@"hh a"];
[dateFormat setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease] ];
NSDate *date1 = [dateFormat dateFromString:startSection];
NSLog(@"Date 1 : %@", date1);
[timeFormat setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease] ];
NSDate *time1 = [timeFormat dateFromString:startSection];
NSString *strSection2 = [NSString stringWithFormat:@"%@", time1];
NSLog(@"Time 1 : %@", strSection2); /// always null at this place.
我不知道我在做什么错在这里。有人能帮我在这里吗?
嘿!你试过我的建议吗?对你起作用吗? – Phlibbo
嗨Phlibbo.Thanks你的帮助。但我的看法不仅仅是看日期。我必须对我必须编写代码的字符串进行排序。 – lifemoveson
对不起,但我不明白你的最后一条评论的一个字:/ – Phlibbo