pankaj
pankaj

Reputation: 8348

Converting NSString to NSDate with date and time being received as different string

I know this have been asked a lot but I am still having the problem in this conversion even after extensive search on the net. Can some one point me to my mistake in following code?

NSString *eventDttm = [[NSString alloc] init];
eventDttm = [NSString stringWithFormat:@"%@ %@",myEvent.startDate,myEvent.timeFrom];
NSLog(@"eventDttm: %@",eventDttm);

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:MM a"];

//conversion of NSString to NSDate

NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [formatter dateFromString:eventDttm];
[formatter release];

NSLog(@"%@",dateFromString);

I am getting date and time separately in string as following:

myEvent.startDate= 2011-11-22
myEvent.timeFrom= 08:00 PM

but dateFromString is always null

Upvotes: 0

Views: 1822

Answers (1)

beryllium
beryllium

Reputation: 29767

Don't mix Months and minutes (MM & mm)

[formatter setDateFormat:@"yyyy-MM-dd HH:mm a"];

Upvotes: 4

Related Questions