简体   繁体   English

将时间字符串解析为NSDate返回nil

[英]Parsing time string into NSDate returns nil

I have time portion as a string and I want to parse it into date, as I have concern about the time portion of date. 我将时间部分作为字符串,我想将其解析为日期,因为我担心日期的时间部分。 But when I parse it with the given format, result is nil. 但是当我用给定的格式解析它时,结果为nil。 How should I parse my Time String ? 我应该如何解析我的时间字符串

Time String: 23:59 时间字符串: 23 59

Method: 方法:

+(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;
}

Result: nil 结果:

Adding the locale worked for me. 添加语言环境对我有用。 I added the following locale. 我添加了以下语言环境。

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

And I got a NSDate object instead of nil. 我得到了一个N​​SDate对象,而不是nil。 Thanks for comment @ZaneHelton 感谢您的评论@ZaneHelton

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM