本文介绍了如何格式化字符串2013-01-27T02:31:47 + 08:00转换为NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已多次尝试将字符串 2013-01-27T02:31:47 + 08:00 转换为NSDate。我找到了并复制其代码并尝试过,但它不起作用。

I have tried many times to convert the string 2013-01-27T02:31:47+08:00 into NSDate. I have found the formatting guide by apple and copied its code and tried , but it doesn't work.

 NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
    NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

    [rfc3339DateFormatter setLocale:enUSPOSIXLocale];
    [rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
    [rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];

    // Convert the RFC 3339 date time string to an NSDate.
    NSDate *date = [rfc3339DateFormatter dateFromString:@"2013-01-27T02:31:47+08:00"];


推荐答案

您的格式应为 @ YYYY-MM-dd'T'HH:MM:ssZZZZZ。您只需要在格式化程序不会使用的字母周围使用单引号。

Your format should be @"yyyy-MM-dd'T'HH:mm:ssZZZZZ". You only need single quotes around letters that are not going to be used by the formatter.

这篇关于如何格式化字符串2013-01-27T02:31:47 + 08:00转换为NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 01:00