Reputation: 1721
I am trying to make the date coming from service as default date and selected also in a uidatepicker. I have tried something, but it is not working.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] ;
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setYear:[[tempBirthDate substringToIndex:4] integerValue]];
[components setMonth:[[tempString substringFromIndex:5] integerValue]];
[components setDay:[[tempBirthDate substringFromIndex:8] integerValue]];
NSDate *defualtDate = [calendar dateFromComponents:components];
[datePicker setDate:defualtDate];
What am i missing?
Could you please help me to figure it out?
for instance : values are coming from the service as 03/19/1986
and i want it selected by picker.
Upvotes: 0
Views: 889
Reputation: 131
You can use NSDateFormatter
for placing date in to the date picker:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateFormat:@"MM/dd/yyyy"];
NSDate *date = [formatter dateFromString:dateFromServer];
[datePicker setDate:date];
Upvotes: 1
Reputation: 1962
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] ;
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setYear:[[tempBirthDate substringWithRange:NSMakeRange(6, 4)] integerValue]];
[components setMonth:[[tempBirthDate substringWithRange:NSMakeRange(0, 2)] integerValue]];
[components setDay:[[tempBirthDate substringWithRange:NSMakeRange(3, 2)] integerValue]];
NSDate *defualtDate = [calendar dateFromComponents:components];
[datePicker setDate:defualtDate];
Upvotes: 3