Reputation: 3527
I need to url encode a date parameter so I can send it to the api.
For example if I have a date 2013-08-01 00:00:00 +0000
, I need to send this through 2013-08-01+00%3a00%3a00+%2b0000
.
How can I achieve it?
Upvotes: 0
Views: 1475
Reputation: 54
This has always worked for me
NSString *encodedDate = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)dateString, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8);
Upvotes: 0
Reputation: 80273
NSDate *myDate; // this date should be encoded
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateFormat = @"yyyy-MM-dd hh:mm:ss ZZZ";
NSString *dateString = [df stringFromDate:myDate];
NSString *encodedDate =
[dateString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Upvotes: 2