如果我使用以下代码:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];   
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm"];
NSDate *myDate = [dateFormatter dateFromString:@"2010-01-28T15:22:23.863"];
NSLog(@"%@", [dateFormatter stringFromDate:myDate]);

它被成功地转换成了Date个对象,然而,我似乎无法用yyyy-MM-dd'T'HH:mm以外的任何方式格式化它,也就是说,记录的是2010-01-28T15:22:23

如果我把dateFormat改为[dateFormatter setDateFormat:@"yyyy-MMMM-d'T'HH:mm"];,Date对象为空...

所以我的最终问题是如何从SQL数据库中格式化ISO8601时间戳,以使用NSDateFormatterMediumStyle返回"2010年1月1日"?

推荐答案

您需要另一个格式化程序来处理输出.把这个放在你的代码后面:

NSDateFormatter *anotherDateFormatter = [[NSDateFormatter alloc] init];   
[anotherDateFormatter setDateStyle:NSDateFormatterLongStyle];
[anotherDateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSLog(@"%@", [anotherDateFormatter stringFromDate:myDate]);

Objective-c相关问答推荐

iOS8:退出应用程序后不久出现蓝条正在使用您的位置

如何在没有 Interface Builder 的情况下创建 Cocoa 接口?

当 UITextField 成为第一响应者时如何使 UIScrollView 自动滚动

如何使用 UIStepper

如何将 CGPoint 添加到 NSMutableArray?

在 iOS 中实现闪屏

pathForResource 返回 null

如何在 iPhone 应用程序的 UINavigationBar 中添加图像

UITableView 无限滚动

ScrollView 手势识别器吃掉所有的事件

如何与 UITableView 一起滚动标题?

iOS 内存分配 - 一个应用程序可以使用多少内存?

我们可以在 UIPageViewController 中自定义页面指示器吗?

如何在 iPhone 上画出讲话泡泡?

自定义 UINavigationBar 背景

从昨天开始无法将存档上传到应用store

主队列上的 dispatch_sync 与 dispatch_async

UITableViewCell 右侧有图像?

iOS 7 中的 UITextView 链接检测

UICollectionView 断言失败