简体   繁体   English

NSDateFormatter dateFromString 和 iPhone 在 24 小时格式混淆

[英]NSDateFormatter dateFromString and iPhone in 24 Hour Format Confusion

I'm having a problem.我有问题。 I get incoming time strings in 12-hour format, and I'm turning them into NSDate objects.我以 12 小时格式获取传入的时间字符串,并将它们转换为 NSDate 对象。 When the iPhone is in 12 hour format, no problem.当 iPhone 为 12 小时制时,没问题。 But when it's in 24 Hour format, things go wrong.但是当它采用 24 小时制时,事情就会出错。 Here's some sample code to demonstrate:下面是一些示例代码来演示:

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];

In 24 hour mode, date is 1970-01-01 03:19:00, and theString is "3:19" - WRONG 24小时模式,日期是1970-01-01 3点19分00秒,而theString是“3:19” -错误

In 12 hour mode, date is 1970-01-01 15:19:00, and theString is "3:19 PM" - RIGHT在 12 小时模式下,日期为 1970-01-01 15:19:00,字符串为“3:19 PM” - RIGHT

So... question 1: why is the device's 24 hour setting overriding my date formatter setting?所以...问题 1:为什么设备的 24 小时设置会覆盖我的日期格式化程序设置?

and more importantly, question 2: How do I get a proper conversion from 12 hour time to 24 hour time?更重要的是,问题 2:如何将 12 小时制正确转换为 24 小时制?

I already have code to detect if the phone is in 24 hour mode, but other than digging around in the string and swapping the 3 with a 15, there doesn't seem to be a clean way to do this.我已经有代码来检测手机是否处于 24 小时模式,但除了在字符串中挖掘并将 3 与 15 交换之外,似乎没有一种干净的方法可以做到这一点。

Not sure if you still need it, but I've had a similar problem which got solved by setting the locale for the date formatter.不确定您是否仍然需要它,但我遇到了类似的问题,通过设置日期格式化程序的语言环境解决了这个问题。 That is, if you want to force it to 12-hour mode, regardless of the user's 24/12 hour mode setting, you should set the locale to en_US_POSIX.也就是说,如果要强制其为 12 小时模式,无论用户的 24/12 小时模式设置如何,都应将 locale 设置为 en_US_POSIX。

The reason for this behaviour is Locale, set the correct 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];

It works for both 24-hour and 12 hour format它适用于 24 小时和 12 小时格式

I changed from @"hh:mm:ss" to @"HH:mm:ss" and time style was changed from " 1:03 PM " to " 13:03 ".我从@"hh:mm:ss"更改为@"HH:mm:ss" ,时间样式从“ 1:03 PM ”更改为“ 13:03 ”。 Hope this will help you.希望这会帮助你。

I believe the @"h:mm a" should be @"HH:mm a".我相信@"h:mm a" 应该是@"HH:mm a"。

If you use the pre-build dateformatter in cocoa, everything will be taken care of for you.如果您在可可中使用预构建日期格式化程序,一切都会为您处理。

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

NSDateFormatterShortStyle and NSDateFormatterNoStyle comes in different varieties. NSDateFormatterShortStyleNSDateFormatterNoStyle有不同的种类。 Using those will make sure you respect the settings the user has selected for dates and times.使用这些将确保您尊重用户为日期和时间选择的设置。

The 12-14 hour clock conversion is taken care of by the SDK, if you have a model or some value object for storing your dates try to keep them as NSDate . 12-14 小时时钟转换由 SDK 负责,如果您有一个模型或一些值对象来存储您的日期,请尝试将它们保留为NSDate This way you can format them only when you need to display them.这样,您可以仅在需要显示它们时对其进行格式化。 Saving dates as strings could open a world of trouble when you maybe parse them from xml where the GMT is specified separately or try to add and subtract NSTimeIntervals .当您可能从 xml 解析它们时,将日期保存为字符串可能会带来麻烦,其中 GMT 是单独指定的,或者尝试添加和减去NSTimeIntervals

Okay, I left a comment, but it squished all the code together, so I'll have to "answer" my question with a comment:好的,我留下了评论,但它将所有代码压缩在一起,所以我必须用评论“回答”我的问题:

Thanks.谢谢。 I gave it a whirl with this code:我用这段代码试了一下:

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]; 

And date comes up nil.日期出现为零。 I ran into this earlier when I tried this route, and it's not working.我早些时候在尝试这条路线时遇到了这个问题,但它不起作用。 Very frustrating.非常令人沮丧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM