NSDate 区域格式问题
NSDateFormatter *dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:@"yyyy-MM-dd hh:mm a"];
NSDate *selectedDate = [[NSDate alloc] init];
selectedDate = [dtFormatter dateFromString:[NSString stringWithFormat:@"%@", [appDelegate.aryTime objectAtIndex:i]]];
NSLog(@"selected Date: %@", selectedDate);
NSLog(@"Today Date: %@", todayDate);
int interval = [todayDate compare:selectedDate];
我有这个代码。现在,发生的情况是,当我运行应用程序时,保持英国的区域格式(从“设置”>“常规”>“国际”>设置区域格式)(他们使用上午和下午),应用程序会很好地比较两个日期。但是,如果我将区域格式更改为瑞典(他们使用 em 和 fm 代替),则不会比较日期。有什么方法可以消除区域格式的依赖性并比较日期?我尝试将它们转换为 24 小时格式,但这没有帮助。
NSDateFormatter *dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:@"yyyy-MM-dd hh:mm a"];
NSDate *selectedDate = [[NSDate alloc] init];
selectedDate = [dtFormatter dateFromString:[NSString stringWithFormat:@"%@", [appDelegate.aryTime objectAtIndex:i]]];
NSLog(@"selected Date: %@", selectedDate);
NSLog(@"Today Date: %@", todayDate);
int interval = [todayDate compare:selectedDate];
I've this code. Now, what happens is while I run the app keeping the region format (From Settings>General>International>set region format) of UK (where they use AM and PM) the app compares both the dates fine. But if I change my region format to Sweden (where they use em and fm instead), the dates are not being compared.Is there any way where I can remove the dependency of region format and compare dates? I tried converting them in 24 hr formats but that didn't help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过以下方式比较两个日期..
==>扩展内置 NSDate 类(.h 以及 .m 文件)
实现类:
从要与日期进行比较的类中,
对两个日期执行以下操作,
在扩展日期类中实现另一种方法,用于比较日期的所有参数整数..
希望它可以帮助你
问候,
尼尔
You can compare two date with following way ..
==> extend in built NSDate Class (.h as well as .m files)
implementation class:
from the class where you want to compare to dates
do following for both dates
implement one more method in extended date class for comparing all the parameter of date which are integer..
hope it can help you
regards,
neil