简体   繁体   中英

NSString (+timezone) to NSDate

failing to convert NSString into NSDate

result: date = nil;

(*NOTE: date receiving from In App Purchase Receipt)

NSString * date_string = @"2017-09-24 11:20:21 Etc/GMT";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:date_string];
NSLog(@"date %@",date);

Try this..

    NSString * date_string = @"2017-09-24 11:20:21 Etc/GMT";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss VV"];
    NSDate *date = [dateFormatter dateFromString:date_string];
    NSLog(@"date %@",date);

The problem is that you haven't told the date formatter how to deal with the funny sequence of characters at the end of the date ie the Etc/GMT . You format needs a time zone specifier at the end of it. VV might do the trick ie

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss VV"];

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