我正在使用NSDateFormatter在iPhone應用程序中工作。我從網絡服務獲得時間,如"00:00:00", "16:00:00","15:30:00" and so on
。如果時間是"00:00:00" to "12 AM"
,如果時間是"16:00:00" to "4 PM"
,如果時間是「15:30:00」到「3.30 PM」,我需要將這些時間轉換爲 。我已經使用了下面的代碼,但是當我通過"16:00:00" the date returns null
。如何獲取上午/下午格式的時間iphone NSDateFormatter?
NSString *dats1 = @"16:00:00";
NSDateFormatter *dateFormatter3 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter3 setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter3 setDateFormat:@"hh:mm:ss"];
NSDate *date1 = [dateFormatter3 dateFromString:dats1];
NSLog(@"date1 : %@", date1); **// Here returning (null)**
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm a"];
NSLog(@"Current Date: %@", [formatter stringFromDate:date1]); **// Here also returning (null)**
[formatter release];
當我路過 「00:00:00」 它返回 「12點」 正確。任何人都可以請幫我解決這個問題嗎?提前致謝。
感謝所有發佈您的答案並幫助我的人。你們都回答正確,並幫助我解決這個問題。再次感謝。 – Gopinath 2012-07-23 07:45:13
我放棄了所有的正確答案。非常感謝您的幫助。 – Gopinath 2012-07-23 07:48:03
真的,你應該花一些時間閱讀「NSDateFormatter」的文檔,你可以自己回答這個問題。 – 2012-07-23 08:16:28