简体   繁体   中英

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. How should I parse my Time String ?

Time String: 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. Thanks for comment @ZaneHelton

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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