NSDate 区域格式问题

发布于 2024-12-25 03:16:19 字数 658 浏览 1 评论 0原文

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 技术交流群。

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

发布评论

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

评论(1

昔梦 2025-01-01 03:16:19

您可以通过以下方式比较两个日期..

==>扩展内置 NSDate 类(.h 以及 .m 文件)

#import <Foundation/Foundation.h>
struct DateInformation {
int day;
int month;
int year;
int weekday;
int minute;
int hour;
int second;
 };
  typedef struct DateInformation DateInformation;
  @interface NSDate (Extra)
   - (DateInformation) dateInformation;
   @end

实现类:

#import "NSDate+Extra.h"

  @implementation NSDate (Extra)

- (DateInformation) dateInformation
   {
DateInformation info;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [gregorian components:(NSMonthCalendarUnit | NSMinuteCalendarUnit | NSYearCalendarUnit |NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit) 
                                      fromDate:self];
info.day = [comp day];
info.month = [comp month];
info.year = [comp year];

info.hour = [comp hour];
info.minute = [comp minute];
info.second = [comp second];

info.weekday = [comp weekday];


[gregorian release];
return info;
    }

@end

从要与日期进行比较的类中,

对两个日期执行以下操作,

 NSDate *date=[NSDate date];
 DateInformation info=[date dateInformation];
   NSLog(@"%@",info);

 NSDate *date1=[NSDate dateWithTimeIntervalSinceNow:1242141];
 DateInformation info=[date1 dateInformation];
   NSLog(@"%@",info);

在扩展日期类中实现另一种方法,用于比较日期的所有参数整数..

希望它可以帮助你

问候,
尼尔

You can compare two date with following way ..

==> extend in built NSDate Class (.h as well as .m files)

#import <Foundation/Foundation.h>
struct DateInformation {
int day;
int month;
int year;
int weekday;
int minute;
int hour;
int second;
 };
  typedef struct DateInformation DateInformation;
  @interface NSDate (Extra)
   - (DateInformation) dateInformation;
   @end

implementation class:

#import "NSDate+Extra.h"

  @implementation NSDate (Extra)

- (DateInformation) dateInformation
   {
DateInformation info;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [gregorian components:(NSMonthCalendarUnit | NSMinuteCalendarUnit | NSYearCalendarUnit |NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit) 
                                      fromDate:self];
info.day = [comp day];
info.month = [comp month];
info.year = [comp year];

info.hour = [comp hour];
info.minute = [comp minute];
info.second = [comp second];

info.weekday = [comp weekday];


[gregorian release];
return info;
    }

@end

from the class where you want to compare to dates

do following for both dates

 NSDate *date=[NSDate date];
 DateInformation info=[date dateInformation];
   NSLog(@"%@",info);

 NSDate *date1=[NSDate dateWithTimeIntervalSinceNow:1242141];
 DateInformation info=[date1 dateInformation];
   NSLog(@"%@",info);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文