3

I am parsing the JSON returned from App Store when verifying a receipt.

One of the public fields is original_purchase_date. However, the date that is returned here is in the format of "2014-05-30 14:05:51 Etc/GMT". The issue being that when I try to compare this, the suffix (Etc/GMT) appears to be causing issues. I am trying to understand how to manage this, as this suffix could be any timezone (say America/Los_Angeles or whatever).

Is there a way of converting this into a useable format?

I know in the receipt there is also a field original_purchase_date_ms which is seconds since 1970 I suspect, however, this is a private field (not called out in any docs), so I don't want to rely on its use, only to find the app rejected, or Apple later remove the field!

Thanks in advance.

NeilMortonNet
  • 1,500
  • 4
  • 16
  • 36
  • truncate the string and remove "Etc/GMT" out of it. then use regular date formatter and get your NSDate – Sam B Jun 06 '14 at 12:48

1 Answers1

2

Sample Code:

NSString *oldDateStr = [NSString stringWithFormat: @"2014-05-30 14:05:51 Etc/GMT"];
NSString *newDateStr = [oldDateStr stringByReplacingOccurrencesOfString:@"Etc/GMT" withString:@""];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date1 = [dateFormat dateFromString:newDateStr];
Sam B
  • 27,273
  • 15
  • 84
  • 121
  • 4
    This will fail if timezone changes. Like `2016-09-29 23:34:34 America/Los_Angeles"`. Use `"yyyy-MM-dd HH:mm:ss VV"` date format instead. – TheTiger Oct 07 '16 at 12:25
  • Maybe you could try the other way round and just keep the first 19 characters. – Oliver Sep 16 '18 at 12:25