iPad 上出现负日期问题,模拟器上则不然

发布于 2024-09-12 09:00:19 字数 599 浏览 0 评论 0原文

我正在开发历史应用程序,因此我需要处理 JC 之前和之后的日期。

我正在尝试解析格式为“01/01/-200”的字符串,但在处理“01/01/200”时返回空日期。

这是我的代码:

NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc]init] autorelease];
[dateFormatter setDateFormat:@"dd/MM/y"]; // @TODO Get negative date
[dateFormatter setLenient:NO];

NSDate* date = [dateFormatter dateFromString:dateString];
return date;

我还尝试使用“01/01/200 BC”的形式 setDateFormat:@"dd/MM/y G" 但我也无法使其工作。 正如 mvds 在他的回答中建议的那样,我在模拟器上尝试了格式“01/01/200 BC”,并且它正在工作......问题仅发生在我的 iPad 上(版本 3.2.1

)您知道如何正确执行此操作吗?

I'm working on an history application so I need to cope with date before and after JC.

I'm trying to parse a string with the form "01/01/-200" but it returns a null date while it's working with "01/01/200".

Here is my code :

NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc]init] autorelease];
[dateFormatter setDateFormat:@"dd/MM/y"]; // @TODO Get negative date
[dateFormatter setLenient:NO];

NSDate* date = [dateFormatter dateFromString:dateString];
return date;

I also try using with the form "01/01/200 BC" setDateFormat:@"dd/MM/y G" but I can't make it work neither.
As mvds suggests in his answer, I tried the format "01/01/200 BC" on the simulator, and it's working... the problem only occurs on my iPad (version 3.2.1)

Do you have an idea how to do this properly ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙气飘飘 2024-09-19 09:00:19

我刚刚尝试了这个:

NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc]init] autorelease];
[dateFormatter setDateFormat:@"dd/MM/y G"];
NSDate *date = [dateFormatter dateFromString:@"01/01/200 BC"];
NSLog(@"refdate %@",[dateFormatter stringFromDate:date]);
date = [date addTimeInterval:24*3600*365*2];
NSLog(@"2 years later %@",[dateFormatter stringFromDate:date]);

输出:

refdate 01/01/200 BC
2 years later 01/01/198 BC

这是在 3.2、iPad 模拟器上,所以不是最新的 SDK,但仍然是 iPad。运行这个你会得到不同的结果吗?

I just tried this:

NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc]init] autorelease];
[dateFormatter setDateFormat:@"dd/MM/y G"];
NSDate *date = [dateFormatter dateFromString:@"01/01/200 BC"];
NSLog(@"refdate %@",[dateFormatter stringFromDate:date]);
date = [date addTimeInterval:24*3600*365*2];
NSLog(@"2 years later %@",[dateFormatter stringFromDate:date]);

which outputs:

refdate 01/01/200 BC
2 years later 01/01/198 BC

This is on 3.2, iPad simulator, so not the most recent SDK, but iPad nonetheless. Do you get different results, running this?

窗影残 2024-09-19 09:00:19

我终于找到窍门了。
问题是我的 iPad 是法语的,所以 Era 有不同的格式:

  • BC 是“av. J.-C”。
  • AD 是“ap.J.-C”。

因此,我只需更改 XML 文件即可在解析时获得正确的格式。

为了以 AD-BC 格式显示我的日期,我只是随后将其转换:

+ (NSString*) convertIntoBCADString:(NSString*) originalString 
{
    NSString* newString = [originalString stringByReplacingOccurrencesOfString:@"av. J.-C." withString:@"BC"];
    return [newString stringByReplacingOccurrencesOfString:@"ap. J.-C." withString:@"AD"]; 
}

I finally find the trick.
The problem is that my iPad is in French so the Era has a different format :

  • BC is "av. J.-C."
  • AD is "ap. J.-C."

So I just had to change my XML file to get the correct format when parsing.

In order to display my date in the AD-BC format, I just convert it afterward :

+ (NSString*) convertIntoBCADString:(NSString*) originalString 
{
    NSString* newString = [originalString stringByReplacingOccurrencesOfString:@"av. J.-C." withString:@"BC"];
    return [newString stringByReplacingOccurrencesOfString:@"ap. J.-C." withString:@"AD"]; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文