Question

I have date in string January-01-2014. I wan to set this date to my date picker. I am doing this to set that date but got error like this:

Assertion failure in -[_UIDatePickerView _setDate:animated:forced:], /SourceCache/UIKit_Sim/UIKit-2903.2/_UIDatePickerView.m:313
2014-01-03 13:17:22.095 GratZeez[1469:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date'

My code is:

df = [[NSDateFormatter alloc] init];

df.dateStyle=NSDateFormatterMediumStyle;
datePicker.date=[df dateFromString:[tempArray objectAtIndex:2]];
Was it helpful?

Solution

Try below code your app cannot get crashed.

df = [[NSDateFormatter alloc] init];
df.dateStyle=NSDateFormatterMediumStyle;
[df setDateFormat:@"MMMM-dd-yyyy"];
datePicker.date=[df dateFromString:[tempArray objectAtIndex:2]];

OTHER TIPS

check this and change dateformat if causing anything change as your time zone too

 NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];  
[formatDate setDateFormat:@"dd-MM-yyyy  HH:mm:ss"];
NSTimeZone *zoneTime=[NSTimeZone defaultTimeZone];
[formate setTimeZone:zoneTime];
NSDate *date = [formate dateFromString:yourDateArray];  
NSLog(@"Output date :%@",date);

[yourDatePicker setDate:date];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top