Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

iphone

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.

like image 903
jp chance Avatar asked Oct 26 '09 20:10

jp chance


1 Answers

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];
like image 58
Epsilon Prime Avatar answered Nov 12 '22 11:11

Epsilon Prime