objective-c – 在iOS中解析RFC3339日期字符串的最简单方法是什么?

objective-c – 在iOS中解析RFC3339日期字符串的最简单方法是什么?,第1张

概述Youtube API以RFC3339格式返回日期字符串.我发现如何在手动上解析它,无论如何,这太长了. - (NSString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString // Returns a user-visible date time string th Youtube API以RFC3339格式返回日期字符串.我发现如何在手动上解析它,无论如何,这太长了.

- (Nsstring *)userVisibleDateTimeStringForRFC3339DateTimeString:(Nsstring *)rfc3339DateTimeString    // Returns a user-visible date time string that corresponds to the    // specifIEd RFC 3339 date time string. Note that this does not handle    // all possible RFC 3339 date time strings,just one of the most common    // styles.{    Nsstring *          userVisibleDateTimeString;    NSDateFormatter *   rfc3339DateFormatter;    NSLocale *          enUSPOSIXLocale;    NSDate *            date;    NSDateFormatter *   userVisibleDateFormatter;    userVisibleDateTimeString = nil;    // Convert the RFC 3339 date time string to an NSDate.    rfc3339DateFormatter = [[[NSDateFormatter alloc] init] autorelease];    enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIDentifIEr:@"en_US_POSIX"] autorelease];    [rfc3339DateFormatter setLocale:enUSPOSIXLocale];    [rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];    [rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];    date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];    if (date != nil) {        // Convert the NSDate to a user-visible date string.        userVisibleDateFormatter = [[[NSDateFormatter alloc] init] autorelease];        assert(userVisibleDateFormatter != nil);        [userVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle];        [userVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle];        userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:date];    }    return userVisibleDateTimeString;}

我可以使一个函数包含这个,但我想知道在Cocoa基础或标准C或POSIX库上是否有预定义的方法来执行此 *** 作.如果它存在,我想用它.你能告诉我有更简单的方法吗?或者,如果您确认这是最简单的方式,将非常感谢:)

解决方法 纯粹的东西 – 与Cocoa一起来的正是你正在做的事情.您可以通过在其他位置(可能在init中)创建日期格式化程序并在此方法中使用/重用它们来使此方法更短,更快. 总结

以上是内存溢出为你收集整理的objective-c – 在iOS中解析RFC3339日期字符串的最简单方法是什么?全部内容,希望文章能够帮你解决objective-c – 在iOS中解析RFC3339日期字符串的最简单方法是什么?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1001241.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存