views:

140

answers:

2

I have a UIDatePicker that shows (ex. April | 13 | 2010). I need to get each component value and set it as an integer so it can be put into three separate keys in a Plist.

MyMonth  Number  04
MyDay    Number  13
MyYear   Number  2010

I can set a text label by using:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
dateLabel.text = [NSString stringWithFormat:@"%@",
                                           df stringFromDate:datePkr.date]];
[df release];

But I just need to convert each component value to integer. Would it be easier creating a UIPickerView with date array and doing it that way?

+2  A: 

You know, a plist can store an NSDate.

To get a component from date picker, generate a NSDateComponents object from the calendar with -components:fromDate::

NSDateComponents* comps = [datePkr.calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit)
                                              fromDate:datePkr.date];
int myYear = [comps year];
int myMonth = [comps month];
int myDay = [comps day];
KennyTM
+1  A: 

Forget about the formatter and just use NSDateComponents - the online docs give a good example of getting integer date components from an NSDate.

Paul Lynch