Thursday, 22 August 2013

iOS: display time as am/pm

iOS: display time as am/pm

I am new to objective c. I wish to do the following: Convert 24 hour
format to 12 hour and then add +2 to hour and display it like: 4:00 pm I
get the 12 hour format but after adding +2 to it , the time is displayed
always as "am", i.e even if it is 4 pm it is displayed as 4 am. Below is
my code:
NSDate *now = [NSDate date];
NSDateFormatter *timeFormatter=[[NSDateFormatter alloc]init];
timeFormatter.dateFormat=@"hh:00 a";
NSString *currentHour=[timeFormatter stringFromDate:now ];
lblcurrentHour.text=[NSString stringWithFormat:@"%@",currentHour];
NSLog(@"%@",currentHour);
int hour=[[timeFormatter stringFromDate:now]intValue];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
dateFormatter1.dateFormat = @"HH:mm";
NSDate *date1 = [dateFormatter1 dateFromString:[NSString
stringWithFormat:@"%02d:00",hour+=3]];
dateFormatter1.dateFormat = @"hh:mm a";
lblnextHour.text = [dateFormatter1 stringFromDate:date1]; // prints 4:00
am not pm
How do i solve this? Where am i getting wrong?

No comments:

Post a Comment