我有一個字符串,其中有日期字段。我將字段作爲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