mark cerqueira well-rounded nerd

Ruby DateTime to Objective-C NSDate

Converting a Ruby DateTime to an Objective-C NSDate will lead you on a wonderful journey where you realize the DateTime outputs itself is incompatible with any potential NSDateFormatter you try to use to parse it. The crux of this is because DateTime includes a colon in its timezone offset which you cannot express in a NSDateFormatter.

So what is one to do? One option is to skip the default formatter of DateTime and roll your own:

# See
my_datetime.strftime('%Y-%m-%d %H:%M:%S')

Then build the corresponding formatter in Objective-C:

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

With this, you too can avoid the fruitless journey of trying to get NSDateFormatter to comprehend DateTime’s default format. Happy interoperating with languages!