【问题标题】:NSDataFormatter dateFromString results in incorrect outputNSDataFormatter dateFromString 导致输出不正确
【发布时间】:2013-02-26 10:32:58
【问题描述】:

这是我的代码:

[dateFormat setDateFormat:@"EEE MM d HH:mm:ss yyyy"];
logDate = [dateFormat dateFromString:[line substringToIndex:24]];    

行由字符串组成:"Mon Feb 18 09:25:53 2013: FAILED : Configuration-Update :"

我只得到日期“2013 年 2 月 18 日星期一 09:25:53”,并将其格式化为 @"EEE MM d HH:mm:ss yyyy" 格式。 我得到一个不正确的输出:"2013-02-18 03:55:53 -0000" Time 单独打印不正确。我关注Date Format Patterns 指定模式,但我仍在逐步解决这个问题。我无法理解哪里出了问题。如果有人发现问题会很有帮助。 提前谢谢

【问题讨论】:

  • 尝试为格式化程序设置时区
  • “不正确的输出”显然是 NSLogging NSDate 对象产生的description 输出,而不是格式化的字符串。你得到的正是人们所期望的。
  • 你设置了格式化程序的行为吗? [NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];

标签: iphone objective-c nsdateformatter


【解决方案1】:

试试这个

NSDate *date = [NSDate date];
NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"EEE MM d hh:mm:ss yyyy"];
NSLog(@"%@",[formatter stringFromDate:date]);
NSLog(@"%@",date);

当您仅使用 NSLog 记录时,您会得到 UTC 格式(UTC 由偏移值组成,在印度其标准时间 +5.30)即 yyyy-MM-d HH:mm:ss z ,z 是添加或减去的偏移量到当前时间或从当前时间出发。

例如

NSLog(@"%@",[formatter stringFromDate:date]);
NSLog(@"%@",date);

Tue 02 26 05:38:00 2013 //"EEE MM d hh:mm:ss yyyy"

2013 年 2 月 26 日星期二 17:46:22 //“EEE MM d HH:mm:ss yyyy”

2013-02-26 12:08:00 +0000 //仅使用 NSLog 进行日志记录

希望对你有帮助

【讨论】:

    【解决方案2】:

    当您像这样打印结果日期时,您需要为 NSDateFormatter 设置时区:

    [formatter setTimeZone:[NSTimeZone localTimeZone]];
    

    【讨论】:

      【解决方案3】:

      你需要用作:

      NSString *line=@"Mon Feb 18 09:25:53 2013: FAILED : Configuration-Update :";
      
      NSDateFormatter *dateFormat=[NSDateFormatter new];
      [dateFormat setDateFormat:@"EEE' 'MM' 'd' 'HH:mm:ss' 'yyyy"];
      
      NSDate *logDate = [dateFormat dateFromString:[line substringToIndex:24]];
      
      NSLog(@"logDate->%@",logDate);
      

      【讨论】:

      • 由于时区不同,它无法正常工作。设置时区后,它起作用了。谢谢
      【解决方案4】:

      如果我们获取 NSdate 对象的日志,它将仅显示相对于时区 +0000 的日期。为此,我们需要将日期格式化为我们当地的时区。像这样。。

      [dateFormat setDateFormat:@"EEE MM d HH:mm:ss yyyy"];
      logDate = [dateFormat dateFromString:@"Mon Feb 18 09:25:53 2013"];
      [dateFormat setTimeZone:[NSTimeZone localTimeZone]];
      NSLog(@"Date %@",[dateFormat stringFromDate:logDate]);
      

      【讨论】:

      • NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init]autorelease]; [dateFormat setDateFormat:@"EEE MM d HH:mm:ss yyyy"]; logDate = [dateFormat dateFromString:[line substringToIndex:24]]; [dateFormat setTimeZone:[NSTimeZone localTimeZone]]; NSLog(@"日期为 %@",logDate);
      • 误以为你使用了 logdate 而不是这个...... NSLog(@"Date %@",[dateFormat stringFromDate:logDate]);
      【解决方案5】:

      感谢大家..它通过这种方式更改代码来工作..

      NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init]autorelease];
      [dateFormat setDateFormat:@"EEE MM d HH:mm:ss yyyy"];    
      [dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];    
      return [dateFormat dateFromString:dateString];
      

      【讨论】:

        猜你喜欢
        • 2015-10-30
        • 1970-01-01
        • 2015-12-14
        • 2016-08-24
        • 1970-01-01
        • 2015-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多