Time Formatting Parsing in Objective-C
Our first example demonstrates time formatting and parsing in Objective-C. Here’s the full source code:
This program demonstrates various ways to format and parse dates in Objective-C:
We start by creating an NSDateFormatter
object, which we’ll use for all our date formatting and parsing operations.
We format the current time according to ISO8601 (similar to RFC3339 in the original example).
We parse a date string using the same format.
We demonstrate custom format strings for both formatting and parsing.
We show how to extract individual components of a date and format them manually.
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:
Then run it with:
The output will be similar to:
Note that the exact output will depend on the current date and time when you run the program.