jp chance
jp chance

Reputation: 559

iOS SDK: How to get the date out of a UIDatePicker?

I need to retrieve the date from a UIDatePicker (Preferably I would also like to be specify the format as well. For example, mmdd would output the string 1209. Any string that reasonably parsed would work as well.

Thanks in advance.

Upvotes: 15

Views: 17315

Answers (1)

Epsilon Prime
Epsilon Prime

Reputation: 4586

You need to use the date property:

NSDate *myDate = datePicker.date;

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"cccc, MMM d, hh:mm aa"];
NSString *prettyVersion = [dateFormat stringFromDate:myDate];

BTW, it's not obvious but you can add specific non-parsed text by encompassing it inside single quotes in the format:

[dateFormat setDateFormat:@"'Game-'yyyyMMdd-HHmm'.xml'"];
NSString *filenameVersion = [dateFormat stringFromDate:myDate];

Upvotes: 41

Related Questions