How to get the time in am/pm format iphone NSDateFormatter?

You can use the .ShortStyle which will adapt to the current locale:

let timeFormatter = NSDateFormatter()
timeFormatter.dateStyle = .NoStyle
timeFormatter.timeStyle = .ShortStyle
timeFormatter.stringFromDate(NSDate())

Which returns 17:12 or 5:12 PM depending on the current device locale.


If your time format is in 24hrs please use "HH:mm:ss" format. Please test the code and let me know if it is working for you. I have tested and it is returning "4 PM"

NSString *dats1 = @"16:00:00";
 NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
 [dateFormatter3 setDateStyle:NSDateFormatterMediumStyle];  
 [dateFormatter3 setDateFormat:@"HH:mm:ss"]; 
 NSDate *date1 = [dateFormatter3 dateFromString:dats1];
 NSLog(@"date1 : %@", date1); **// Here returning (null)**

 [dateFormatter setDateFormat:@"hh:mm a"];
 NSLog(@"Current Date: %@", [formatter stringFromDate:date1]); **// Here also returning (null)**
 [formatter release];

Thanks.