Skip to content

Instantly share code, notes, and snippets.



Created Feb 15, 2013
What would you like to do?
NSDateFormatter bug
// dateString expected format: "2012-08-08 17:31:01 Etc/GMT"
// This value comes from our receipt-verification service,
// so it's independent of any of the user's settings
+(NSDate*)translateExpirePurchaseStringToDate: (NSString*)dateString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// strip the ending
NSString *purchaseDateString = [dateString stringByReplacingOccurrencesOfString:@" Etc/GMT" withString:@""];
// set the gmt timezone just in case
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// -- Here was my bug:
// The purchaseDate value assignment below is nil when the user's
// preferences are: UK Calendar, London timezone with 24-hour time OFF.
// Having the user turn 24-hour time on in their phone's preferences
// yielded an NSDate just fine.
// It wasn't what I'd expected, since the input value is totally
// independent of the user's settings. :'(
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *purchaseDate = [dateFormatter dateFromString:purchaseDateString];
// ... do some other unrelated things
return purchaseDate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment