Dealing with military time (and some regular expression stuff too)

Had to convert military time into AM/PM. I thought there would be an easier way to do it, but couldn't find anything. Here's what I did:


First, I wrote a method to parse military time into an NSDateComponents object. Note that this uses a regular expression, which is a little tricky in iOS. Also, you could adjust the regex to parse AM/PM time into an NSDateComponents object.

- (NSDateComponents *) parseTimeStringIntoDateComponents: (NSString*) str {


NSDateComponents *dc = nil;

NSString *pattern = @"^([0-9]{2}):([0-9]{2})$";

NSRegularExpression *regex;

NSRange strRange = NSMakeRange(0, [str length]);

NSError *error = NULL;


regex = [NSRegularExpression regularExpressionWithPattern:pattern   options:NSRegularExpressionCaseInsensitive 



if (error) {

LogError(@"Error parsing time into date components - %@", [error description]);



int numberOfMatches = [regex numberOfMatchesInString:str options:NSRegularExpressionCaseInsensitive range:strRange];

//NSLog(@"str: %@   matches:%d", str, numberOfMatches);


if (numberOfMatches > 0) {


NSTextCheckingResult *match = [regex firstMatchInString:str options:NSRegularExpressionCaseInsensitive range:strRange];

int hrs = [[str substringWithRange:[match rangeAtIndex:1]] intValue];

int min = [[str substringWithRange:[match rangeAtIndex:2]] intValue];

//NSLog(@"TimeString: %@  -  hrs:%d, min:%d",str,hrs,min);

dc = [[NSDateComponents alloc] init];

[dc setHour:hrs];

[dc setMinute:min];


} else {

LogError(@"Time string is not formatted properly and cannot be parsed into date components");


return dc;



I also had to display the time in AM/PM format, so I wrote this method take the NSDateComponent and parse it into a string:

-(NSString*)convertTimeFromMilitary: (NSDateComponents*)dc{

// convert military time to AM/PM

int mHr = dc.hour;

int min = dc.minute;


int hr;

if(mHr == 0) {

hr = 12;

}else {

hr = mHr % 12;



return [NSString stringWithFormat:@"%d:%.2d %@", hr, min, (mHr >= 12 ? @"PM" : @"AM")];




Add a Comment...



Hi there, just wanted to tell you, I loved this post. It absolutely was funny. Go on posting! - 9/28/2016

It's remarkable in favor of me to have a web site, which is good in support of my experience. thanks admin - 9/12/2016

Can you tell us more about this? I'd want to find out more details. - 8/30/2016

payfirstsolutions thegioiyoga russiansoverseas chiyou ogir technocreats edusofttec dickemabout voteselfie privateturk - 5/08/2017

Every weekend i utilized to pay a visit this website, as i wish for enjoyment, because this this website conations genuinely nice funny information too. - 12/16/2016

When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get three emails with the same comment. Is there any way you can remove people from that service? Appreciate it! - 12/01/2016

This post will help the world wide web people for creating new site or even a blog from begin to end. - 11/15/2016

It really is the very best a chance to develop plans in the future and it's a chance to be at liberty. We have read this post and if I really could I wish to suggest you some interesting things or tips. Perhaps you can write next articles referring to this post. I wish to read much more things about it! - 10/12/2016

Great beat ! I prefer to apprentice while you amend your site, how could i subscribe for any weblog website? The account helped me a applicable deal. I were a little bit acquainted of this your broadcast offered bright clear concept - 10/05/2016

At this moment I am just disappearing to do my breakfast, once having my breakfast coming again to see more news. - 10/02/2016