skip to Main Content

I have a string in my dictionary "hoursOfOperation" from a service that has an NSString in this format: "09:00 AM – 05:30 PM" and the hours change for each object (can be 8 am – 4 pm etc)

How can I re-format this so that it becomes "9 a.m.-5:30 p.m." in objective-C?

I was able to change the am/pm by just using stringByReplacingOccurencesOfString but I am not sure how to reformat the time. Right now it looks like "09:00 a.m. – 05:30 p.m."

2

Answers


  1. In short, to format a date/time, you need a NSDateFormatter.
    In your case, there are several steps:

    1. Divide the "09:00 AM – 05:30 PM" into 2 different NSString variables
    2. For each NSString variable, you need to convert it to NSDate and use NSDateFormatter to set the date format then convert it back to your desired date string
    3. Add those two NSString that were just formatted to an array
    4. Join those those objects together into a final time string.

    Code sample:

    NSString *yourString = @"09:00 AM - 05:30 PM";
    NSArray *timeComponents = [yourString componentsSeparatedByString:@" - "];
    NSMutableArray *formattedTimeComponents = [NSMutableArray array];
    for (NSString *timeComponent in timeComponents) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"hh:mm a"];
        
        NSDate *time = [dateFormatter dateFromString:timeComponent];
        [dateFormatter setDateFormat:@"h:mm a"];
        dateFormatter.AMSymbol = @"a.m.";
        dateFormatter.PMSymbol = @"p.m.";
        NSString *formattedTime = [dateFormatter stringFromDate:time];
        [formattedTimeComponents addObject:formattedTime];
    }
    NSString *formattedHoursOfOperationString = [formattedTimeComponents componentsJoinedByString:@"-"];
    
    Login or Signup to reply.
  2. You should use NSDateIntervalFormatter to produce a result that localizes correctly and properly handles time zones.

    You really, really, really shouldn’t be manually messing with AM/PM formats yourself because that won’t localize properly.

    To do that, you’ll need to calculate the correct start & end dates to use from the original string.

    NSDateFormatter *parseFormatter = [[NSDateFormatter alloc] init];
    // You should probably set the timeZone for the formatter
    parseFormatter.dateStyle = NSDateFormatterNoStyle;
    parseFormatter.timeStyle = NSDateFormatterShortStyle; // This might not be the correct style. You should test this with your actual data.
    NSArray *times = [originalTimes componentsSeparatedByString:@" - "];
    NSDate *startDate = [parseFormatter dateFromString:times[0]];
    NSDate *endDate = [parseFormatter dateFromString:times[1]];
    

    Next, create a date interval formatter, which will properly localize the range for you.

    NSDateIntervalFormatter *intervalFormatter = [[NSDateIntervalFormatter alloc] init];
    formatter.dateStyle = NSDateIntervalFormatterNoStyle;
    formatter.timeStyle = NSDateIntervalFormatterShortStyle;
    // You may also want to set timeZone for time zone you want to be shown in
    NSString *result = [formatter stringFromDate:startDate toDate:endDate];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search