Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDateFormatter timezone (Etc/GMT) not getting parsed for dateFromString

I am getting nil for trying to convert this 2011-07-11 13:28:59 Etc/GMT into a NSDate by the following code

NSString *dateString=@"2011-07-11 13:28:59 Etc/GMT";
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"yyyy-MM-dd hh:mm:ss z"];
NSDate *purchaseDate = [dateformatter dateFromString:dateString];

This date is coming from apple server itself for in-app purchase validation. I've gone about lot of threads with timezone. but none of them show how to convert this Etc/GMT

i have tried z zz zzz zzzz Z ZZ ZZZ ZZZZ

what might be the issue here.

like image 815
darshansonde Avatar asked Jul 11 '11 13:07

darshansonde


3 Answers

The correct dateFormat is yyyy-MM-dd HH:mm:ss VV which is what you should use to solve your problem.

like image 66
Rudolf Adamkovič Avatar answered Nov 08 '22 06:11

Rudolf Adamkovič


// Swift Version

    let DateString = "2016-01-21 00:29:09 Etc/GMT"
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss VV"
    var dateObject = dateFormatter.date(from: DateString)
like image 22
iOS Developer Avatar answered Nov 08 '22 04:11

iOS Developer


NSString *dateString=@"2011-07-11 13:28:59 Etc/GMT";
dateString = [dateString stringByReplacingOccurrencesOfString:@"Etc/GMT" withString:@"GMT"];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];

// Or stuff....>>

NSDate *date = [dateformatter dateFromString: dateString];
NSLog(@"You may Need This  =========%@",date);
dateformatter = [[[NSDateFormatter alloc] init] autorelease];
[dateformatter setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];

NSString *convertedString = [dateformatter stringFromDate:date];
NSLog(@"Converted String : %@",convertedString);

/// may help =====>> http://unicode.org/reports/tr35/

like image 2
kalpesh jetani Avatar answered Nov 08 '22 05:11

kalpesh jetani