【问题标题】:NSDateFormatter dateFromString and iPhone in 24 Hour Format ConfusionNSDateFormatter dateFromString 和 iPhone 的 24 小时格式混淆
【发布时间】:2010-02-01 00:08:12
【问题描述】:

我遇到了问题。我得到 12 小时格式的传入时间字符串,并将它们转换为 NSDate 对象。当 iPhone 是 12 小时格式时,没问题。但是当它是 24 小时制时,事情就出错了。下面是一些示例代码来演示:

NSString *theTime = @"3:19 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"h:mm a"]; // "3:19 PM"
NSDate *date = [formatter dateFromString:theTime];
NSString *theString = [formatter stringFromDate:date];

在 24 小时模式下,date 是 1970-01-01 03:19:00,theString 是“3:19” - 错误

在 12 小时模式下,date 是 1970-01-01 15:19:00,theString 是 "3:19 PM" - 对

那么...问题 1:为什么设备的 24 小时设置会覆盖我的日期格式化程序设置?

更重要的是,问题 2:如何将 12 小时制正确转换为 24 小时制?

我已经有代码来检测手机是否处于 24 小时模式,但除了在字符串中挖掘并将 3 与 15 交换之外,似乎没有一种干净的方法来做到这一点。

【问题讨论】:

    标签: iphone cocoa-touch time nsdateformatter


    【解决方案1】:

    不确定您是否仍然需要它,但我遇到了类似的问题,通过设置日期格式化程序的语言环境得到了解决。也就是说,如果你想强制它进入 12 小时模式,无论用户的 24/12 小时模式设置如何,你都应该将语言环境设置为 en_US_POSIX。

    【讨论】:

    • 这个答案是在我选择了另一条路线很久之后才出现的,但听起来很合理,而且有人已经验证过,所以我将其标记为已回答。
    • 一个不那么明显的结论:让系统的语言环境通过普通的[[NSDateFormatter alloc] init]-created 日期格式化程序。将此格式化程序用于您向用户显示的任何字符串。如果您想从始终为 12 或 24 小时时间的源(例如 app const 或服务器)获取字符串,请在不同的格式化程序上使用 .locale = [[NSLocale alloc] initWithLocaleIdentifier:@“en_US_POSIX"] 以确保它们不会被虐待。请记住,日期格式化程序控制 12/24 小时时间表达式,而 NSDates 没有,一切都会好起来的。
    【解决方案2】:

    这种行为的原因是Locale,设置正确的Locale

    NSString *strAgendaDate = @"01/17/2012 12:00 AM"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]; [dateFormatter setDateFormat:AgendaDateFormatForMeeting]; NSDate *meetingDate = [dateFormatter dateFromString:aStrDate]; [dateFormatter setDateFormat:AgendaDateRepresentation]; strAgendaDate = [dateFormatter stringFromDate:meetingDate];

    它适用于 24 小时和 12 小时格式

    【讨论】:

    • 谢谢@Meet,一行代码可以改变一切真是太疯狂了:)
    【解决方案3】:

    我相信@"h:mm a" 应该是@"HH:mm a"。

    如果您在 cocoa 中使用预构建日期格式化程序,一切都会为您处理好。

    NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [timeFormatter setTimeStyle:NSDateFormatterShortStyle];
    [timeFormatter setDateStyle:NSDateFormatterNoStyle];
    

    NSDateFormatterShortStyleNSDateFormatterNoStyle 有不同的种类。 使用这些将确保您尊重用户为日期和时间选择的设置。

    SDK 会处理 12-14 小时时钟转换,如果您有用于存储日期的模型或一些值对象,请尝试将它们保留为 NSDate。这样,您可以仅在需要显示它们时对其进行格式化。将日期保存为字符串可能会带来麻烦,因为您可能会从单独指定 GMT 的 xml 中解析它们,或者尝试添加和减去 NSTimeIntervals

    【讨论】:

    • 谢谢。我用这段代码试了一下: NSString *theTime = @"3:19 PM"; NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease]; [timeFormatter setTimeStyle:NSDateFormatterShortStyle]; [timeFormatter setDateStyle:NSDateFormatterNoStyle]; NSDate *date = [timeFormatter dateFromString:theTime]; NSString *theString = [timeFormatter stringFromDate:date];日期为零。当我尝试这条路线时,我早些时候遇到了这个问题,但它不起作用。非常令人沮丧。
    【解决方案4】:

    我从 @"hh:mm:ss" 改为 @"HH:mm:ss" 时间样式从 "1:03下午”到“13:03”。 希望这会对你有所帮助。

    【讨论】:

      【解决方案5】:

      好的,我留下了评论,但它把所有代码挤在一起了,所以我必须用评论“回答”我的问题:

      谢谢。我用这段代码试了一下:

      NSString *theTime = @"3:19 PM"; 
      NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease];       
      [timeFormatter setTimeStyle:NSDateFormatterShortStyle]; 
      [timeFormatter setDateStyle:NSDateFormatterNoStyle]; 
      NSDate *date = [timeFormatter dateFromString:theTime]; 
      NSString *theString = [timeFormatter stringFromDate:date]; 
      

      日期为零。当我尝试这条路线时,我早些时候遇到了这个问题,但它不起作用。非常沮丧。

      【讨论】:

      • 嗨,Silromen。该方法仅用于显示日期和时间。要从字符串构建日期,您需要应用正确的格式。就像你在这里做的那样:[格式化程序 setDateFormat:@"h:mm a"]; // "3:19 PM" 但请注意 HH 而不是 hh。
      • 感谢您的回复,但这不起作用。在 24 小时模式的 iPhone 上,下午 3:54 仍然作为 03:54:00 日期对象出现,而在模拟器中(我的计算机时钟处于 24 小时模式),情况更糟:12:54:00。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多