views:

90

answers:

2

Hi all,

I'm trying to set the date of a UIDatePicker but I the month is everytime "January"(01). I tryed to use "MM" and "LL", but both of them are not working.

My code:

-(void)viewDidLoad {    
    [super viewDidLoad];

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

    [dateFormat setDateFormat:@"YYYY-MM-DD"];;
    NSDate *anyDate = [dateFormat dateFromString:@"2019-09-11"];   
    [datePicker setDate:anyDate];

    [dateFormat release];

}

Thanks in advance,

Claudio

A: 

Are you getting a warning on the setDate call?

It looks like the signature is "setDate:animated:"

What happens when you do:

[datePicker setDate:anyDate animated:NO];

?

ajh158
no, there is no warning about it.
Claudio
+1  A: 

Mac's and iPhones use the Unicode Date Formatting.

So your code should be:

-(void)viewDidLoad {    
[super viewDidLoad];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

[dateFormat setDateFormat:@"yyyy'-'MM'-'dd"];;
NSDate *anyDate = [dateFormat dateFromString:@"2019-09-11"];   
[datePicker setDate:anyDate];

[dateFormat release];
}

Your data formatter used

  • 'YYYY' - "Year (of "Week of Year"), used in ISO year-week calendar. May differ from calendar year." (whatever that means...)
  • 'DD' - "Day of year" which is most likely why it gives you the January result.
Shane Powell
Great! It works! Thank you very much!
Claudio