0

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);
Ofir Malachi
  • 1,145
  • 14
  • 20
  • "(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 Sep 28 '17 at 15:12
  • @CRD `Etc/GMT` is a valid time zone. – vadian Sep 28 '17 at 15:13
  • @vadian - thanks, just learnt something. – CRD Sep 28 '17 at 15:14
  • All about `Etc/GMT` https://stackoverflow.com/questions/7303580/understanding-the-etc-gmt-time-zone – JeremyP Sep 28 '17 at 15:16

2 Answers2

1

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);
Nirav Kotecha
  • 2,493
  • 1
  • 12
  • 26
1

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"];
JeremyP
  • 84,577
  • 15
  • 123
  • 161