【问题标题】:dateFromString always returns null with dateformatterdateFromString 始终使用 dateformatter 返回 null
【发布时间】:2011-04-12 16:17:16
【问题描述】:

我有以下代码:

 NSDateFormatter * df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [df setTimeZone:[NSTimeZone systemTimeZone]];
    [df setFormatterBehavior:NSDateFormatterBehaviorDefault];

    [df dateFromString:@"2011-04-12 10:00:00"];

它总是生成一个空日期。这是为什么呢?

【问题讨论】:

标签: iphone objective-c


【解决方案1】:

如果设备设置为上午/下午时间并且请求的字符串格式设置为@"yyyy-MM-dd HH:mm:ss"dateFromString 将返回nil

如果您将语言环境设置为@"en_US",则转换将返回正确的日期。

dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:@"2013-02-14 09:30:00"];

【讨论】:

  • 即使进行检查并将格式设置为“hh:mm a”也不起作用。这样做。谢谢!
【解决方案2】:

您是在后台线程中执行此操作吗?当我没有在 ui 线程中使用 NSDateFormatter 时,我有过奇怪的经历。无论如何,这是我使用的方法,应该适合你:

+ (NSDate*)parseDate:(NSString*)inStrDate format:(NSString*)inFormat {
    NSDateFormatter* dtFormatter = [[NSDateFormatter alloc] init];
    [dtFormatter setLocale:[NSLocale systemLocale]];
    [dtFormatter setDateFormat:inFormat];
    NSDate* dateOutput = [dtFormatter dateFromString:inStrDate];
    [dtFormatter release];
    return dateOutput;
}

【讨论】:

    【解决方案3】:

    我有同样的问题。我首先使用this resource 仔细检查用于转换的字符串是否有效。

    然后我想到它可能是语言环境,所以我尝试了以下方法:

    [dateFomatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_AU"]]

    它就像一个魅力。希望这会有所帮助。

    【讨论】:

      【解决方案4】:

      我刚刚运行了这段代码,它对我有用。

      NSDateFormatter * df = [[NSDateFormatter alloc] init];
      [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
      [df setTimeZone:[NSTimeZone systemTimeZone]];
      [df setFormatterBehavior:NSDateFormatterBehaviorDefault];
      
      NSDate *theDate = [df dateFromString:@"2011-04-12 10:00:00"];
      NSLog(@"date: %@", theDate);
      

      输出为:日期:2011-04-12 17:00:00 +0000

      【讨论】:

      • 你忘记了:NSDate *theDate =
      【解决方案5】:

      是的,同意@Jan Gressmann,这太奇怪了!我也在非 ui 线程上使用 NSDateFormatter,并且 dateFromString 选择器有时返回 null ......例如,我有这个 code:

      NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
      NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
      [dateFormat setLocale:usLocale];
      [dateFormat setDateFormat:@"dd MMM yy HH:mm:ss"];
      NSDate *startDate = [dateFormat dateFromString:str_StartDate];
      NSDate *endDate = [dateFormat dateFromString:str_endDate];
      

      在控制台中输出

      (lldb) po str_StartDate
      (NSString *) $7 = 0x06da2180 03 SEP 2012 10:00:00
      (lldb) po str_endDate
      (NSString *) $8 = 0x06d3a810 08 SEP 2013 10:00:00
      (lldb) po startDate
      (NSDate *) $9 = 0x00000000 <nil>
      (lldb) po endDate
      (NSDate *) $10 = 0x06db05b0 2013-09-08 02:00:00 +0000
      

      这真的很奇怪......

      【讨论】:

        【解决方案6】:

        我在 iOS8 (XCode 6) 上遇到了类似的问题,我在 NSDateFormatter 中做了以下更改 -

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        
        [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        
        NSDate *formattedDate = [dateFormatter dateFromString:dateString];
        

        【讨论】:

        • 设置时区缩写“UTC”帮助了我。谢谢
        【解决方案7】:

        dateFromStringstringFromDate 返回 null 因为 device 时间格式(例如:12 小时或 24 小时)和 设备时区

        将 12 小时转换为 24 小时

        - (NSString*)getTimeIn24Hr:(NSString*)time12hrWithDate
        {
            NSDateFormatter* df12 = [[NSDateFormatter alloc] init];
            [df12 setTimeZone:[NSTimeZone systemTimeZone]];
            [df12 setDateFormat:@"yyyy-MM-dd hh:mm a"];
            NSDate* dateTime12hr = [df12 dateFromString:time12hrWithDate];
        
            NSDateFormatter *df24 = [[NSDateFormatter alloc]init];
            [df24 setLocale:[NSLocale systemLocale]];
            [df24 setDateFormat:@"HH:mm:ss"];
            NSString * time24HrOutput = [df24 stringFromDate:dateTime12hr];
        
            return time24HrOutput;
        }
        

        将 24 小时转换为 12 小时

        - (NSString*)getTimeIn12Hr:(NSString*)time24hrWithDate
        {
            NSDateFormatter* df24 = [[NSDateFormatter alloc] init];
            [df24 setLocale:[NSLocale systemLocale]];
            [df24 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSDate* dateTime24hr = [df24 dateFromString:time24hrWithDate];
        
            NSDateFormatter *df12 = [[NSDateFormatter alloc]init];
            [df12 setDateFormat:@"hh:mm a"];
            NSString * time12HrOutput = [df12 stringFromDate:dateTime24hr];
        
            return time12HrOutput;
        }
        

        【讨论】:

          【解决方案8】:

          我和你有类似的问题,dateFromString: 在 ios 模拟器中成功,而在 iPhone 中,它总是返回 NULL,在我添加下面附有 dateFormatter 的代码后,它在 iPhone 广告模拟器中成功:

          [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
          

          【讨论】:

            【解决方案9】:

            在转换(日期到字符串)和(字符串到日期)时,尝试将两个日期格式化程序设置为相同的格式。格式如下

            [formatter1 setDateFormat:@"MM/dd/yyyy hh:mm:ss a"]
            
            [formatter2 setDateFormat:@"MM/dd/yyyy hh:mm:ss a"]
            

            【讨论】:

              【解决方案10】:

              您可以使用这种格式"yyyy-MM-dd hh:mm:ss"

              在iOS10上使用swift2.3时,关键是使用小写的hh而不是HH来格式化小时

              希望这可能有用。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2013-11-23
                • 2016-05-31
                • 2015-08-15
                • 2012-03-18
                • 2016-11-04
                • 2016-07-28
                • 2010-11-08
                相关资源
                最近更新 更多