比较 NSDate 的某些组件?

发布于 2024-10-23 22:41:45 字数 33 浏览 2 评论 0原文

我如何仅比较 2 个 NSDates 的年月日组件?

How would I compare only the year-month-day components of 2 NSDates?

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

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

发布评论

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

评论(4

溺深海 2024-10-30 22:41:45

因此,您可以这样做:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger desiredComponents = (NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit);

NSDate *firstDate = ...; // one date
NSDate *secondDate = ...; // the other date

NSDateComponents *firstComponents = [calendar components:desiredComponents fromDate:firstDate];
NSDateComponents *secondComponents = [calendar components:desiredComponents fromDate:secondDate];

NSDate *truncatedFirst = [calendar dateFromComponents:firstComponents];
NSDate *truncatedSecond = [calendar dateFromComponents:secondComponents];

NSComparisonResult result = [truncatedFirst compare:truncatedSecond];
if (result == NSOrderedAscending) {
  //firstDate is before secondDate
} else if (result == NSOrderedDescending) {
  //firstDate is after secondDate
}  else {
  //firstDate is the same day/month/year as secondDate
}

基本上,我们获取两个日期,去掉它们的小时-分钟-秒位,然后将它们转回日期。然后我们比较这些日期(不再有时间部分;只有日期部分)并查看它们之间的比较情况。

警告:在浏览器中输入但未编译。警告实施者

So here's how you'd do it:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger desiredComponents = (NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit);

NSDate *firstDate = ...; // one date
NSDate *secondDate = ...; // the other date

NSDateComponents *firstComponents = [calendar components:desiredComponents fromDate:firstDate];
NSDateComponents *secondComponents = [calendar components:desiredComponents fromDate:secondDate];

NSDate *truncatedFirst = [calendar dateFromComponents:firstComponents];
NSDate *truncatedSecond = [calendar dateFromComponents:secondComponents];

NSComparisonResult result = [truncatedFirst compare:truncatedSecond];
if (result == NSOrderedAscending) {
  //firstDate is before secondDate
} else if (result == NSOrderedDescending) {
  //firstDate is after secondDate
}  else {
  //firstDate is the same day/month/year as secondDate
}

Basically, we take the two dates, chop off their hours-minutes-seconds bits, and the turn them back into dates. We then compare those dates (which no longer have a time component; only a date component) and see how they compare to eachother.

WARNING: typed in a browser and not compiled. Caveat implementor

好菇凉咱不稀罕他 2024-10-30 22:41:45

查看此主题 NSDate 获取年/月/日

一旦你拉出日期/月/年,您可以将它们作为整数进行比较。

如果您不喜欢这种方法

,您可以尝试这个..

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy"];
int year = [[dateFormatter stringFromDate:[NSDate date]] intValue];

[dateFormatter setDateFormat:@"MM"];
int month = [[dateFormatter stringFromDate:[NSDate date]] intValue];

[dateFormatter setDateFormat:@"dd"];
int day = [[dateFormatter stringFromDate:[NSDate date]] intValue];
  • 还有另一种方法...

    NSDateComponents *dateComp = [日历组件:unitFlags fromDate:date];
    
    NSInteger 年 = [dateComp 年];
    
    NSInteger 月份 = [dateComp 月份];
    
    NSInteger day = [dateComp day];
    

Check out this topic NSDate get year/month/day

Once you pull out the day/month/year, you can compare them as integers.

If you don't like that method

Instead, you could try this..

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy"];
int year = [[dateFormatter stringFromDate:[NSDate date]] intValue];

[dateFormatter setDateFormat:@"MM"];
int month = [[dateFormatter stringFromDate:[NSDate date]] intValue];

[dateFormatter setDateFormat:@"dd"];
int day = [[dateFormatter stringFromDate:[NSDate date]] intValue];
  • And another way...

    NSDateComponents *dateComp = [calendar components:unitFlags fromDate:date];
    
    NSInteger year = [dateComp year];
    
    NSInteger month = [dateComp month];
    
    NSInteger day = [dateComp day];
    
白馒头 2024-10-30 22:41:45

从 iOS 8 开始,您可以使用 NSCalendar-compareDate:toDate:toUnitGranularity: 方法。

像这样:

    NSComparisonResult comparison = [[NSCalendar currentCalendar] compareDate:date1 toDate:date2 toUnitGranularity:NSCalendarUnitDay];

Since iOS 8 you can use -compareDate:toDate:toUnitGranularity: method of NSCalendar.

Like this:

    NSComparisonResult comparison = [[NSCalendar currentCalendar] compareDate:date1 toDate:date2 toUnitGranularity:NSCalendarUnitDay];

使用 -[NSDate Compare:] 方法 - NSDate 比较参考

With the the -[NSDate compare:] method - NSDate Compare Reference

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