Biranchi
Biranchi

Reputation: 16327

NSDateFormatting Issue in ObjectiveC

I have the following piece of code,

NSDate *date = nil;
NSString *departureTime = @"Mon Oct 24 00:00:00 GMT 2011";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[df setDateFormat:@"EEE MMM dd hh:mm:ss GMT yyyy"];
date = [df dateFromString:departureTime];
[df release];

NSLog(@"Date : %@", date);

I am always getting nil, What is wrong in this code ?

Upvotes: 1

Views: 81

Answers (2)

rikky
rikky

Reputation: 76

You are geting nil just because of date format you have set -> "EEE MMM dd hh:mm:ss GMT yyyy"

If you cannot provide right format for the date it will not provide you a valid output.

For valid format parameter to get date in your format for click Me

Upvotes: 1

Ilanchezhian
Ilanchezhian

Reputation: 17478

While setting the date format, the time zone format is not proper.

Change the line as follows:

[df setDateFormat:@"EEE MMM dd hh:mm:sss Z yyyy"];

Upvotes: 1

Related Questions