Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone: NSDateFormatter how to show time in 24 hours format?

NSDateFormatter *formatter1=[[NSDateFormatter alloc]init];
[formatter1 setDateFormat:@"HHmmss MMddyyyy"];
NSDate *finalDate =[formatter1 dateFromString:testTime];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[lblpickUpTime setText:[formatter stringFromDate:finalDate]];
[formatter release];
[formatter1 release];

where testTime = @"201518 07122011"; and I want the result in 24 hours time

e.g. 20.15 rather than 8:15 PM

and is there any better way to do this?

like image 810
Azhar Avatar asked Aug 24 '11 11:08

Azhar


1 Answers

Try by setting

[formatter setDateFormat:@"HH:mm"];  // replacement for [formatter setTimeStyle:NSDateFormatterShortStyle];

Alternate method (without using any formatters):

NSRange range;
range.location = 2;
range.length = 2;
NSString *dateString = [NSString stringWithFormat:@"%@:%@",[myString substringToIndex:2],[myString substringWithRange:range]];
like image 133
KingofBliss Avatar answered Oct 20 '22 00:10

KingofBliss