两个 NSDate 的问题

发布于 2024-11-15 00:35:12 字数 458 浏览 4 评论 0原文

我对这段代码有疑问:

NSDate *today = [[NSDate alloc] init];
if (dateOne == today) {
dataLabel.textColor = [UIColor redColor]; NSLog(@"inside if");}
NSLog(@"today:%@", oggi);
NSLog(@"dateOne:%@", dateOne);

控制台的结果是

2011-06-10 17:19:00.170 Project[678:707] today:2011-06-10 15:19:00 +0000
2011-06-10 17:19:00.174 Project[678:707] dateOne:2011-06-10 15:19:00 +0000

我也尝试“isEqualToDate”,但代码没有输入“if”内

I have a problem with this code:

NSDate *today = [[NSDate alloc] init];
if (dateOne == today) {
dataLabel.textColor = [UIColor redColor]; NSLog(@"inside if");}
NSLog(@"today:%@", oggi);
NSLog(@"dateOne:%@", dateOne);

the result of console is

2011-06-10 17:19:00.170 Project[678:707] today:2011-06-10 15:19:00 +0000
2011-06-10 17:19:00.174 Project[678:707] dateOne:2011-06-10 15:19:00 +0000

I also try "isEqualToDate" but the code don't entry inside "if"

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

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

发布评论

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

评论(2

画尸师 2024-11-22 00:35:12

我了解您正在尝试确定给定的 NSDate 是否是今天。为此,您无需在比较中考虑小时、分钟等。怎么样:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:[NSDate date]];
NSDate *today = [calendar dateFromComponents:components];
components = [calendar components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:otherDate];
NSDate *dateOne = [calendar dateFromComponents:components];

if([today isEqualToDate:dateOne]) {
    ...
}

方法 isEqualToDate: 不会执行这个工作,因为它检查给定日期是否与接收者完全相等(具有亚秒精度)。

== 运算符也不适合这种情况,因为它将指针与对象进行比较,它检查指针是否指向同一内存位置。

I understand you are trying to determine if a given NSDate is today. To do this you don't have to take in consideration hours, minutes, etc. in your comparison. How about this:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:[NSDate date]];
NSDate *today = [calendar dateFromComponents:components];
components = [calendar components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:otherDate];
NSDate *dateOne = [calendar dateFromComponents:components];

if([today isEqualToDate:dateOne]) {
    ...
}

The method isEqualToDate: won't do this job because it checks whether a given date is exactly equal the receiver (with sub-second precision).

The == operator is not appropriate for this either because it compares the pointers to the objects, it checks whether the pointers point to the same memory location.

み零 2024-11-22 00:35:12

== 比较指针以查看它们是否指向同一个对象,而不是对象本身的值。要比较日期,请使用 -[NSDate isEqualToDate:]

if ([dateOne isEqualToDate:today]) {
    dataLabel.textColor = [UIColor redColor];
    NSLog(@"inside if");
}

== compares pointers to see if they point to the same object -- not the values of the object themselves. To compare dates, use -[NSDate isEqualToDate:]:

if ([dateOne isEqualToDate:today]) {
    dataLabel.textColor = [UIColor redColor];
    NSLog(@"inside if");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文