【问题标题】:NSString to NSDate conversion returns nullNSString 到 NSDate 的转换返回 null
【发布时间】:2013-08-05 12:32:12
【问题描述】:

NSStringNSDate 的转换返回null

原始字符串输出“2013-07-16T18:42:56+02:00”,但当我尝试转换为NSDate 时,它返回“null”

这是我的代码:

// original string
NSString *str = [timeStamp objectAtIndex:indexPath.row];

// convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

//[dateFormat setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss'+11:00'"];
[dateFormat setDateFormat:@"YYYY-MM-dd'T'HH:mm:ssZZZ"]; // updated from Stavash's post below. Still null is outputted for the date though
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Melbourne"]];
NSDate *dte = [dateFormat dateFromString:str];

cell.timeStampLabel.text = [NSString stringWithFormat:@"%@",dte ];

【问题讨论】:

    标签: ios objective-c nsstring nsdate


    【解决方案1】:

    为什么要将 +11:00 硬编码到格式中?我认为这对你有用(在 iOS6.0+ 上):

    [dateFormat setDateFormat:@"yyyy-MM-dd\'T\'HH:mm:ssZZZZZ"];
    

    对于 iOS5.0+ 使用:

    [dateFormat setDateFormat:@"yyyy-MM-dd\'T\'HH:mm:ssZZZ"];
    

    更新

    请参阅下面 Rob 的 cmets - 在 iOS5.0+ 上,日期格式仅适用于没有冒号的时区。

    【讨论】:

    • 谢谢。我改变了它,但输出仍然是“null”。这个 NSString *str = [timeStamp objectAtIndex:indexPath.row];输出 2013-07-16T18:42:56+02:00 所以我知道字符串没问题,它只是转换。
    • ZZZZZ 适用于 iOS6 及更高版本。
    • 另外YYYY 应该是yyyy, cldr.unicode.org/translation/date-time。并且您在不需要设置本地的日期字符串中传递时区。这仅在您想打印日期时很方便。只需从字符串中的时区中删除 : 并使用 Z 这将适用于大多数 iOS 版本。
    • @Stavash:我认为没有其他选择。请参阅上面的 rckoenes 评论:您必须手动删除到冒号。
    • @Stavash 这特别奇怪,因为 ISO8601/RFC3339 日期格式明显早于 iOS5。不过,如果您需要 iOS5 支持,我认为您必须先对日期字符串进行笨拙的编辑。
    【解决方案2】:

    您指定日期的方式不正确

    修正后的代码写在下面

    NSString *str = @"2013-07-16T18:42:56+0200";
    // convert to date
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"YYYY-MM-dd'T'HH:mm:ssZZZ"];    [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Melbourne"]];
    NSDate *dte = [dateFormat dateFromString:str];
    

    【讨论】:

    • 他在时区中有一个冒号。可悲的是,它有所作为。
    【解决方案3】:
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"YYYY-MM-dd'T'HH:mm:ssZZZ"];
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Melbourne"]];
    
    NSString *dateString = @"2013-07-16T18:42:56+02:00";
    NSArray * dateArray = [dateString componentsSeparatedByString:@"+"];
    NSString * timeZone = [dateArray lastObject];
    timeZone = [timeZone stringByReplacingOccurrencesOfString:@":" withString:@""];
    dateString = [NSString stringWithFormat:@"%@+%@",[dateArray objectAtIndex:0], timeZone];
    
    NSDate *dte = [dateFormat dateFromString:dateString];
    

    试试这个...希望它对你有用..我这样做的主要原因是ZZZ会理解+0200,它不会理解+02:00。为了让 DateFormatter 理解 +02:00 我们需要使用 TZD,不幸的是这也导致 null :( 参考 :http://www.w3.org/TR/NOTE-datetime

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-26
      • 2013-09-07
      • 2011-04-24
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多