Date format not working - iphone

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]];

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]];

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];

Related

Cant set default date for datePicker properly, crashing on iOS device [duplicate]

This question already has an answer here:
Invalid parameter not satisfying: date
(1 answer)
Closed 9 years ago.
I have created a post several hours ago, but that was incorrect, I was mistaken about the thing that cause crash. So I have repost it, I hope you will not blame me for that.
I got a view that own a datePicker, my point is to show NOT current date at picker, but show "old" date (I choose 1987 year). But I have no idea what goes wrong, when I launch app on simulator it works perfectly fine, but when I switch to iPhone app crash before view loads. There is the code:
-(void)defaultBirthdayPickerDate{
NSString *dateString = #"09-Oct-1987";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = #"dd-MMM-yyyy";
NSDate *date = [dateFormatter dateFromString:dateString];
[self.birthdayPicker setDate:date];
}
Then:
- (void)viewDidLoad
{
[self defaultBirthdayPickerDate];
[super viewDidLoad];
}
There is the thing, somewhere something cause a crash:
2013-03-05 15:50:31.070 DeathLine[2985:907] *** Assertion failure in -[_UIDatePickerView _setDate:animated:forced:], /SourceCache/UIKit/UIKit-2380.17/_UIDatePickerView.m:302
2013-03-05 15:50:31.073 DeathLine[2985:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date'
*** First throw call stack:
(0x325042a3 0x3a22197f 0x3250415d 0x32dd9ab7 0x344bdf15 0x344c6429 0x3458f989 0x14a6b 0x148db 0x3432b595 0x3438014b 0x34380091 0x3437ff75 0x3437fe99 0x3437f5d9 0x3437f4c1 0x3436db93 0x28f4c33 0x3436d833 0x343f70c5 0x343f7077 0x343f7055 0x343f690b 0x343f6e01 0x3431f5f1 0x3430c801 0x3430c11b 0x360225a3 0x360221d3 0x324d9173 0x324d9117 0x324d7f99 0x3244aebd 0x3244ad49 0x360212eb 0x34360301 0xcc21 0x3a658b20)
libc++abi.dylib: terminate called throwing an exception
Somehow [self.birthdayPicker.date] is not NULL, I know it, but I have no idea how to fix an issue.
My guess is this is caused by the diffent locale (NSLocale) set on your device and in the simulator. Note that 09-Oct-1987 date can be parsed by your formatter only if the formatter locale is set to English language (since Oct stands for October in English). Otherwise it will return nil and setting nil to the UIDatePicker will result in the error you are seeing.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en"];
date is probably nil. You should check if you formatter is working as expected.
try this one
-(void)defaultBirthdayPickerDate{
[self.birthdayPicker setDatePickerMode:UIDatePickerModeDate];
NSString *dateString = #"09-Oct-1987";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MMM-yyyy"];
if([dateFormat dateFromString: dateString])
{
NSDate *date = [dateFormat dateFromString: dateString];
[self.birthdayPicker setDate:date];
}
[dateFormat release];
}
- (void)viewDidLoad{
[super viewDidLoad]; //super method should be first in you code
[self defaultBirthdayPickerDate];
}
try to set DatePickerMode to UIDatePickerModeDate this may solve your issue

How to set the Dateformat for current date in iphone application?

I am developing one application. In that i write the below code for setting the DateFormat for current date. But it is working upto 9th month only. From 10th month onwards it gives the nil value. So please tell me how to solve this one. My code is as below:
NSDate *datestr = clInfo.cldrinfodate;
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"MM/dd/yyyy hh:mma"];
NSString *date1 = [dateFormat1 stringFromDate:datestr];
you can try this one.
NSDate * mCurrentDate = [NSDate date];
NSLog(#"current date=%#",mCurrentDate);
NSDateFormatter *dt=[[NSDateFormatter alloc] init];
[dt setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *st=[dt stringFromDate:mCurrentDate];

NSDateFormatter return null

I'm trying to format a NSDate to a string with date format of yyyyMMdd, here's the code
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd"];
NSDate *date =data.createdTime;
NSLog(#"%#",date);
NSLog(#"%#",[dateFormatter stringFromDate:date]);
the NSLog return me this value
Tue, 24 May 2011 0:05:01 +0800
(null)
Anyone know which part is wrong?
Thanx in advance.
Perhaps currentArticle.createdTime is a valid date but data.createdTime is nil? In addition, you are performing the -setDateFormat: selector on dateFormat, but it seems like your date formatter is dateFormatter.
Try the following code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyyMMdd"];
NSDate *date = data.createdTime;
NSLog(#" Normal Date = %#", date);
NSLog(#"Formatted Date = %#", [dateFormatter stringFromDate: date]);
[dateFormatter release];
If date is nil, then both NSLog() calls will tell you.
Edit
Double check that data.createdTime is an NSDate instance. Perhaps it is an instance of another class, such as NSString, whose -description returns the displayed date. This would explain why NSLog() “shows” the date, but the formatter is returning nil.
BOOL isDate = [data.createdTime isKindOfClass: [NSDate class]];
NSLog(#"Date %# a date.", isDate ? #"is" : #"is not");
[dateFormat setDateFormat:#"yyyyMMdd"];
Should be:
[dateFormatter setDateFormat:#"yyyyMMdd"];
If it still isn't working then something is wrong with data.createdTime; because I ran this code and recieved the expected output:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMdd"];
NSDate *date = [NSDate date];
NSLog(#"%#",date);
NSLog(#"%#",[dateFormatter stringFromDate:date]);

iphone dateFromString giving warning message 'NSDate' may not respond to '+ variable name'

warning: 'NSDate' may not respond to '+currDate'
Above is a warning message when I compile the following code:
-(IBAction)getDate:(id)sender {
currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[ dateFormatter setDateFormat:#"yyyy-MM-dd"];
strCurrDate = [dateFormatter stringFromDate:[NSDate currDate]];
displayDate.text = strCurrDate;
[dateFormatter release];
}
I'm just trying to get the current date and display it in a label called displayDate.
Using the debugger I can see that the date is never converted to a string and stored into strCurrDate.
The warning message is on the line where I try to use stringFromDate
Can anyone see why this isn't working properly? Any help would be greatly appreciated.
I also currDate is my header file:
NSDate *currDate;
That's because currDate is not a class method of NSDate instead it is a property of your class. So this line:
strCurrDate = [dateFormatter stringFromDate:[NSDate currDate]];
should be:
strCurrDate = [dateFormatter stringFromDate:currDate];

NSDate dateFromString deprecated?

I'm trying to use the NSDate dateFromString method but I'm getting an warning and it's crashing the app. The code looks like:
NSString *pickerDate = [NSString stringWithFormat:#"%#", timeSelector.date];
NSDate *defaultDate = [NSDate dateFromString:pickerDate];
The warning is:
'NSDate' may not respond to '+dateFromString'.
It appears that method is deprecated (in the midst of an upgrade from XCode 2 to 3.
What alternate method can I use to create a date from a string?
NSDateFormatter is the intended way for you to get an NSDate from an NSString.
The most basic usage is something like this:
NSString *dateString = #"3-Aug-10";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = #"d-MMM-yy";
NSDate *date = [dateFormatter dateFromString:dateString];
I had a same problem.
I changed the dateFormat from #"YYYY/M/d H:m:s" to #"YYYY/MM/dd HH:mm:ss" as follows.
Then it works on my iPhone 4. (Xcode 4.5 for iOS 6.)
NSDateFormatter *dateFormatter1;
NSString *dateStr1=#"";
NSDate *gantanDate1;
dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter1 setLocale:[NSLocale systemLocale]];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
dateFormatter1.dateFormat=#"YYYY/MM/dd HH:mm:ss";
dateStr1=#"2012/1/1 00:00:00"];
// in his case, dateStr1=pickerDate;
gantanDate1 = [dateFormatter1 dateFromString:dateStr1];
Hi Silber everyone says the same thing to convert the string date to date object.Try this i think you have used two date formatters in two places where you are saving date to string and getting date from string.right try to use the same date formatters in both places. It will solve your problem.