Time Formatting Parsing in Objective-C

Our first example demonstrates time formatting and parsing in Objective-C. Here’s the full source code:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        
        // Here's a basic example of formatting a time
        // according to ISO8601, which is similar to RFC3339
        NSDate *now = [NSDate date];
        [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
        NSString *formattedDate = [formatter stringFromDate:now];
        NSLog(@"%@", formattedDate);
        
        // Time parsing uses the same format as formatting
        NSString *dateString = @"2012-11-01T22:08:41+00:00";
        NSDate *parsedDate = [formatter dateFromString:dateString];
        NSLog(@"%@", parsedDate);
        
        // You can use custom formats for both formatting and parsing
        [formatter setDateFormat:@"h:mm a"];
        NSLog(@"%@", [formatter stringFromDate:now]);
        
        [formatter setDateFormat:@"EEE MMM d HH:mm:ss yyyy"];
        NSLog(@"%@", [formatter stringFromDate:now]);
        
        [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];
        NSLog(@"%@", [formatter stringFromDate:now]);
        
        // Parsing with a custom format
        [formatter setDateFormat:@"h mm a"];
        NSDate *customParsedDate = [formatter dateFromString:@"8 41 PM"];
        NSLog(@"%@", customParsedDate);
        
        // For purely numeric representations you can also use
        // standard string formatting with the extracted components of the time value
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:now];
        
        NSLog(@"%04ld-%02ld-%02ldT%02ld:%02ld:%02ld-00:00",
              (long)components.year, (long)components.month, (long)components.day,
              (long)components.hour, (long)components.minute, (long)components.second);
        
        // Parsing will return nil on malformed input
        [formatter setDateFormat:@"EEE MMM d HH:mm:ss yyyy"];
        NSDate *invalidDate = [formatter dateFromString:@"8:41PM"];
        if (!invalidDate) {
            NSLog(@"Error: Invalid date format");
        }
    }
    return 0;
}

This program demonstrates various ways to format and parse dates in Objective-C:

  1. We start by creating an NSDateFormatter object, which we’ll use for all our date formatting and parsing operations.

  2. We format the current time according to ISO8601 (similar to RFC3339 in the original example).

  3. We parse a date string using the same format.

  4. We demonstrate custom format strings for both formatting and parsing.

  5. We show how to extract individual components of a date and format them manually.

  6. Finally, we show how parsing an invalid date string results in a nil value.

To run this program, save it as TimeFormattingParsing.m and compile it with:

$ clang -framework Foundation TimeFormattingParsing.m -o TimeFormattingParsing

Then run it with:

$ ./TimeFormattingParsing

The output will be similar to:

2023-05-15T10:30:45+0000
2012-11-01 22:08:41 +0000
10:30 AM
Mon May 15 10:30:45 2023
2023-05-15T10:30:45.000+0000
2000-01-01 20:41:00 +0000
2023-05-15T10:30:45-00:00
Error: Invalid date format

Note that the exact output will depend on the current date and time when you run the program.