tags:

views:

340

answers:

2

Hi,

I want to give, for example, 12/11/2005 in the format of mm/dd/yyyy. Can I initialize 12/11/2005 to NSDate directly? Any help?

it gives a warning and crashes when I declare it as

NSDate *then = [NSDate dateWithNaturalLanguageString:02/11/2009  locale:nil];
A: 

Use + (id)dateWithNaturalLanguageString:(NSString *)string

http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSDate%5FClass/Reference/Reference.html#//apple%5Fref/occ/clm/NSDate/dateWithNaturalLanguageString:

sosborn
it gives warning...crashes.....when i declare asNSDate *then = [NSDate dateWithNaturalLanguageString:02/11/2009 locale:nil];
Mikhail Naimy
You need to pass it an NSString.NSDate *then = [NSDate dateWithNaturalLanguageString:@"02/11/2009" locale:nil];
sosborn
+3  A: 

What you need to do is something like this:

NSDateFormatter *mmddccyy = [[NSDateFormatter alloc] init];
mmddccyy.timeStyle = NSDateFormatterNoStyle;
mmddccyy.dateFormat = @"MM/dd/yyyy";
NSDate *d = [mmddccyy dateFromString:@"12/11/2005"];
NSLog(@"%@", d);
Zoran Simic