Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert 24 hr String time into 12 hr time String format

I have time in NSString like "15:15"

I want to covert this NSString time into 12 hr NSString(i.e "3:15 PM").

like image 712
python Avatar asked Jun 21 '12 14:06

python


2 Answers

Use NSDateFormatter to turn the string in a NSDate. Then use the dateformatter again to change the NSDate to a string.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"HH:mm";
NSDate *date = [dateFormatter dateFromString:@"15:15"];

dateFormatter.dateFormat = @"hh:mm a";
NSString *pmamDateString = [dateFormatter stringFromDate:date];

Code might contain some errors, written without compiling or testing.

like image 123
rckoenes Avatar answered Sep 23 '22 01:09

rckoenes


@python

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:@"hh:mm"];

Hope this helps!

like image 26
Ariven Nadar Avatar answered Sep 24 '22 01:09

Ariven Nadar