0
votes

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);
2
"(string contains timezone)" - yet your format contains nothing for the timezone, have you looked up the documentation for date and time formats for timezone items? Is there really "Etc/" in your string? Just seems surprising. - CRD
@CRD Etc/GMT is a valid time zone. - vadian
@vadian - thanks, just learnt something. - CRD

2 Answers

1
votes

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 i.e. the Etc/GMT. You format needs a time zone specifier at the end of it. VV might do the trick i.e.

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

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);