2015-04-02 55 views
0

我有時間部分作爲一個字符串,我想解析它到日期,因爲我擔心日期的時間部分。但是當我用給定的格式解析它時,結果是零。我應該如何解析我的時間字符串解析時間字符串到NSDate返回無

時間字符串: 23:59

方法:

+(NSDate*)getDateFromStringProfile:(NSString*)strDate 
{ 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]; 
    [dateFormat setDateFormat:@"HH:mm"]; 
    [dateFormat setTimeZone:timeZone]; 


    NSDate *result =[dateFormat dateFromString:strDate]; 
    return result; 
} 

結果:

+1

嘗試使用'NSLocale'爲'NSDataFormatter'設置語言環境。 – 2015-04-02 20:48:51

+0

請參閱http://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal-with-the-nsdateformatter-locale-feature – 2015-04-03 01:03:43

回答

0

添加的區域設置爲我工作。我添加了以下語言環境。

[dateFormat setLocale: [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]]; 

而且我得到了一個N​​SDate對象而不是零。謝謝你的評論@ZaneHelton

+2

您應該使用en_US_POSIX。 – rmaddy 2015-04-02 21:28:27