使用 nsdate 比较两个日期

发布于 2024-12-12 09:18:36 字数 466 浏览 0 评论 0原文

在我的应用程序中,我必须比较两次:当前(即 12:44)和考虑到我尝试过:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm"];
NSDate *neededDate = [[NSDate alloc] init];
neededDate = [dateFormatter dateFromString:@"14:45"];
if ([neededDate compare:[NSDate date]] == NSOrderedAscending) {
NSLog(@"yes");
} else NSLog(@"no");
[neededDate release];
[dateFormatter release];

但任何时候我启动它,NSLog 都会说“不”。请指出我哪里错了。先感谢您。

In my app I have to compare two times: current (which is 12:44) and given I tried:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm"];
NSDate *neededDate = [[NSDate alloc] init];
neededDate = [dateFormatter dateFromString:@"14:45"];
if ([neededDate compare:[NSDate date]] == NSOrderedAscending) {
NSLog(@"yes");
} else NSLog(@"no");
[neededDate release];
[dateFormatter release];

But any time I launch it, NSLog says "no". Please, suggest where I' m wrong. Thank you in advance.

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

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

发布评论

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

评论(3

成熟的代价 2024-12-19 09:18:36

因为您的 neededDate 将等于 “1970/01/01 14:45”

您需要设置正确的年份和日期。

例如,您可以使用下一种方法获取当前年份、月份:

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]];
NSLog(@"Current year: %d", components.year);
NSLog(@"Current month: %d", components.month);
NSLog(@"Current day: %d", components.day);

Because your neededDate will be equal to "1970/01/01 14:45".

You need to set correct year and date.

You can get current year, month and say, for example, using next approach:

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]];
NSLog(@"Current year: %d", components.year);
NSLog(@"Current month: %d", components.month);
NSLog(@"Current day: %d", components.day);
旧话新听 2024-12-19 09:18:36

我认为问题在这里:

[dateFormatter setDateFormat:@"hh:mm"];

应该是:

[dateFormatter setDateFormat:@"HH:mm"];

因为现在对于“14:45”它返回(空)日期

(这是对之前答案的补充:)

I think the problem is here:

[dateFormatter setDateFormat:@"hh:mm"];

should be:

[dateFormatter setDateFormat:@"HH:mm"];

because now for "14:45" it returns (null) date

(this is additionally to previous answer :)

等风也等你 2024-12-19 09:18:36

这意味着在您的情况下,needDate 始终高于当前日期。
这可能是因为您以 24 小时格式存储 48 小时字符串。
还将字符串更改为 HH:mm

,验证您存储在 NeededDate 中的值。

这是

NSDate *neededDate = [[NSDate date] dateByAddingTimeInterval:5];
    if ([neededDate compare:[NSDate date]] == NSOrderedAscending) {
        NSLog(@"asc");
    } else {
        NSLog(@"dec");
    }

本例中的一个小示例,将打印 dec。

it means that in your case, neededDate is always higher than the current date.
this is probably because you store a 48 hour string in a 24 hour format.
change the string to HH:mm

also, verify the value you store in neededDate.

here's a small example

NSDate *neededDate = [[NSDate date] dateByAddingTimeInterval:5];
    if ([neededDate compare:[NSDate date]] == NSOrderedAscending) {
        NSLog(@"asc");
    } else {
        NSLog(@"dec");
    }

in this case, dec will be printed.

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