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?
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]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With