Reputation: 7484
trying to use a uidatepicker. have a method that fires when the value of the datepicker changes. it passes the date value, but when i try and take that date, my days portion is acting goofy. it returns the number of days since the beginning of the year, not the actual month day. so say February 3rd, instead of 3, i get 34.
-(IBAction)datePickerValueChanged: (id)sender;
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-DD"];
self.date = [dateFormatter stringFromDate:[sender date]];
NSLog(@"date: %@", [dateFormatter stringFromDate:[sender date]]);
[dateFormatter release];
}
for april 12th, i get:
2011-10-27 14:22:41.939 Satshot[12789:40b] date02: 2011-04-102
i'm guessing my dateformatter is causing it, but i don't understand why.
Upvotes: 2
Views: 459
Reputation: 8772
You are using the wrong format specifier. You need "yyyy-MM-dd"
http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns
Update, YYYY and DD are both wrong. Thanks @jrturton.
Upvotes: 4
Reputation: 119292
From the Data Formatting Guide:
It uses yyyy to specify the year component. A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of "Week of Year"), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.
In addition, DD
should be dd
.
Though why not just use the date to hold your value internally?
Also, note that creating a date formatter is an expensive exercise, valueChanged on a picker view could be called tens of times a second as the user scrolls through. If you must use a formatter, cache it.
Upvotes: 3