How to convert time in 24 hr format in iphone?

iphone, xcode

Solution

 Item *item=[sectionItems objectAtIndex:itemId];
  NSLog(@"%@",item.TimeStart);

NSString *str = @"1:45.PM";// put here item.TimeStart

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"hh:mm.a"];// here give the format which you get in TimeStart

NSDate *date = [dateFormatter dateFromString: str];

dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"HH:mm"];

NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(@"Converted String : %@",convertedString);

Problem

I have to convert the given time in 24 hr format.But I did not get it.Don't know where I m going wrong. If I have TimeStart as 1:15 PM then I should get 13:15 .. ``` Item *item=[sectionItems objectAtIndex:itemId]; NSLog(@"%@",item.TimeStart); NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"HH:mm a"]; NSString *formattedDateString = [dateFormatter stringFromDate:item.TimeStart]; NSLog(@"%@",formattedDateString); ``` In the code above I have a Item class and that I have `NSString` *TimeStart; When I see formattedDateString it is returning null.How can I fix it ?

Original source