Wednesday, November 3, 2010

How to use NSDate with Amazon Query API

In this post i am including a snippet that shows how to format an NSDate so it can be used as a value for the "Expires" key in a query that wil be sent to the Amazon web service. The standard format for the date is like so: "YYYY-MM-DDThh:mm:ssZ", the "Z" stands for the time zone designator (Z or +hh:mm or -hh:mm). Snippet:

NSDate *date = [[[NSDate alloc] init] autorelease];

date = [date dateByAddingTimeInterval:120];

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];

[dateFormatter setDateFormat:@"yyyy-MM-dd"];

NSString *date1 = [dateFormatter stringFromDate:date];

[dateFormatter setDateFormat:@"HH:mm:ss"];

NSString *date2 = [dateFormatter stringFromDate:date];

[NSString stringWithFormat:@"%@T%@-07:00", date1, date2]

No comments: