【问题标题】:Not able to convert NSString to NSDate [closed]无法将 NSString 转换为 NSDate [关闭]
【发布时间】:2013-05-02 11:01:15
【问题描述】:

我从链接中获取日期(Thu May 02 10:08:08 +0000 2013)并存储到字符串中。我想将此字符串转换为 NSDate。

我正在使用此代码:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MMMM-dd hh:MM:ss"];

NSDate *message_Date=[formatter dateFromString:str];

NSLog(@"message_Date ========%@",message_Date);

但 message_Date 打印为空值。

所以请指导我并放一个示例代码。

【问题讨论】:

  • 你的“str”里有什么?
  • 这里的 MMMM 是什么?先是 Google 好友,然后提问
  • 您没有使用与您的日期格式匹配的日期格式有什么原因吗?
  • str 是 Thu May 02 10:08:08 +0000 2013

标签: iphone ios objective-c ios6 nsdate


【解决方案1】:

请尝试使用这个

        NSString *str = @"Thu May 02 10:08:08 +0000 2013";

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"EEE MMM dd HH:mm:ss Z yyyy"];

        NSDate *message_Date=[formatter dateFromString:str];

        NSLog(@"message_Date ========%@",message_Date);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 2011-04-24
    • 2017-04-23
    • 2021-09-11
    相关资源
    最近更新 更多