有没有一种简单的方法可以将 ISO8601 时间戳转换为格式化的 NSDate? [英] Is there a simple way of converting an ISO8601 timestamp to a formatted NSDate?

查看:33
本文介绍了有没有一种简单的方法可以将 ISO8601 时间戳转换为格式化的 NSDate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用以下代码:

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

It is successfully converted to a Date object, however, I cannot seem to format it any other way than yyyy-MM-dd'T'HH:mm, i.e. what gets logged is 2010-01-28T15:22:23

如果我将 dateFormat 更改为 [dateFormatter setDateFormat:@"yyyy-MMMM-d'T'HH:mm"]; 日期对象为空...

If I change the dateFormat to say [dateFormatter setDateFormat:@"yyyy-MMMM-d'T'HH:mm"]; the Date object is null...

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

So my ultimate question is how to format an ISO8601 timestamp from a SQL database to use, for instance, NSDateFormatterMediumStyle to return "January 1, 2010"?

推荐答案

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

You need another formatter to handle the output. Put this after your code:

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

这篇关于有没有一种简单的方法可以将 ISO8601 时间戳转换为格式化的 NSDate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆