ios developer ios developer - 1 month ago 7
iOS Question

HH:MM am/pm not fetch from nsdate

I want to fetch the hours:minutes AM/PM from the nsdate.

NSDate *globalDateTime; //globaldate is : 2016-10-20 06:49:00 +0000 (UTC)
NSString *dateString12 = [NSDateFormatter localizedStringFromDate:globalDateTime
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterFullStyle];
NSLog(@"%@",dateString12);


Result I am getting is NSString format: 20/10/16, 2:03:00 PM India Standard Time

Expected result is NSString format: 20/10/16, 6:49:00 PM India Standard Time

Or is there any way I only get expected result in NSString format : 6:49 PM

dateString12
is get in NSLog is something in UTC format or some other format. It is giving me proper date but not giving me proper time.

Please help me how should I get proper time.

Answer

Try this.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm a"];// change here as per your need
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
NSString *stringFromDate = [formatter stringFromDate: globalDateTime];