2 NSDateComponents - 是同一周吗?

发布于 2024-09-15 18:36:41 字数 989 浏览 6 评论 0原文

我有一个带有 BOOL 返回值和 2 个输入 (NSDateComponents *) 参数的函数。 我的问题是我有两个 NSDateComponents 值,我想知道这两个日期是否在同一日历周内。我尝试了最简单的解决方案来解决问题,我的想法如下:

- (BOOL)isFunctionName:(NSDateComponents *)comp1 和Param:(NSDateComponents *)comp2 { return (([comp1 周] == [comp2 周]) && ([comp1 年] == [comp2 年])); }

但这是不正确的。 我可以用什么方法解决它?


编辑

后,我有一个函数可以从日期中生成日期组件。

<代码> -(NSDateComponents *)dateToDateComponents:(NSDate *)日期 { 无符号的unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *dateComponents = [公历组件:unitFlags fromDate:date]; [公历发布]; 返回日期组件; }

我这样称呼它:

if ([self isFunctionName: [self dateToDateComponents:startDate] 和 Param:[self dateToDateComponents:currentTripDate]]){ }

在我的测试过程中,它返回 YES 我的所有日​​期(例如 2010.07.21 - 2010.08.18)

i have got a function with BOOL return value and 2 input (NSDateComponents *) parameters.
My trouble is I have two NSDateComponents values and I want to know if the two date fall within the same calendar week. i tried the simplest solutions to solve problem, my idea was the following:


- (BOOL)isFunctionName:(NSDateComponents *)comp1 andParam:(NSDateComponents *)comp2 {
return (([comp1 week] == [comp2 week]) && ([comp1 year] == [comp2 year]));
}

but it's not correct.
what way i can solve it ?


edited

so i have a function which makes datecomponents from dates.


-(NSDateComponents *)dateToDateComponents:(NSDate *)date {
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:unitFlags fromDate:date];
[gregorian release];
return dateComponents;
}

and i call it this way:


if ([self isFunctionName: [self dateToDateComponents:startDate] and Param:[self dateToDateComponents:currentTripDate]]){
}

and during my test it returns YES all of my dates (for example 2010.07.21 - 2010.08.18)

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

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

发布评论

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

评论(3

涫野音 2024-09-22 18:36:41

这是受到罗伯特回答的启发。我对其进行了一些调整,以解决一周跨越两年的问题,例如 12/31/2012 和 2012 年 12 月 31 日。 2013 年 1 月 1 日。

- (BOOL)isOnSameWeekAsDate:(NSDate*)otherDate
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    unsigned unitFlags = NSCalendarUnitYearForWeekOfYear | NSCalendarUnitWeekOfYear;

    NSDateComponents *components = [calendar components:unitFlags fromDate:self];
    NSDateComponents *otherComponents = [calendar components:unitFlags fromDate:otherDate];

    return ( [components yearForWeekOfYear] == [otherComponents yearForWeekOfYear] && [components weekOfYear] == [otherComponents weekOfYear] );
}

This is inspired by Robert's answer. I tweaked it a bit to address the issue when a week crosses two years, e.g. 12/31/2012 & 1/1/2013.

- (BOOL)isOnSameWeekAsDate:(NSDate*)otherDate
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    unsigned unitFlags = NSCalendarUnitYearForWeekOfYear | NSCalendarUnitWeekOfYear;

    NSDateComponents *components = [calendar components:unitFlags fromDate:self];
    NSDateComponents *otherComponents = [calendar components:unitFlags fromDate:otherDate];

    return ( [components yearForWeekOfYear] == [otherComponents yearForWeekOfYear] && [components weekOfYear] == [otherComponents weekOfYear] );
}
暖心男生 2024-09-22 18:36:41

NSDateComponent 类参考指出:

重要提示:NSDateComponents 对象本身是没有意义的;你
需要知道它是什么日历
解释反对,你需要
知道这些值是否是绝对的
单位值或数量
单位。

比较 NSDate 怎么样?

- (BOOL) weekIsEqual:(NSDate *)date and:(NSDate *)otherDate {
  NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

  NSDateComponents *dateComponents      = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit fromDate:date];
  NSDateComponents *otherDateComponents = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit fromDate:otherDate];

  return [dateComponents week] == [otherDateComponents week] && [dateComponents year] == [otherDateComponents year];
}

编辑
在这一行中:

 unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;

您没有传递 NSWeekCalendarUnit,因此 [dateComponent week] 返回 NSUndefinedDateComponent

edit

当然,它必须是 NSWeekCalendarUnit...

The NSDateComponent class reference states:

Important: An NSDateComponents object is meaningless in itself; you
need to know what calendar it is
interpreted against, and you need to
know whether the values are absolute
values of the units, or quantities of
the units.

What about comparing NSDates instead?

- (BOOL) weekIsEqual:(NSDate *)date and:(NSDate *)otherDate {
  NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

  NSDateComponents *dateComponents      = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit fromDate:date];
  NSDateComponents *otherDateComponents = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit fromDate:otherDate];

  return [dateComponents week] == [otherDateComponents week] && [dateComponents year] == [otherDateComponents year];
}

edit:
In this line:

 unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;

You don't pass NSWeekCalendarUnit, therefore [dateComponent week] returns NSUndefinedDateComponent

edit

Of course, it has to be NSWeekCalendarUnit

溺深海 2024-09-22 18:36:41

从 iOS8 开始,这变得更加容易:

-(BOOL)isDate:(NSDate *)date1 onSameWeekAs:(NSDate *)date2
{
    return [[NSCalendar currentCalendar] isDate:date1
                                    equalToDate:date2
                              toUnitGranularity:NSCalendarUnitWeekOfYear];
}

文档

如果两个日期的所有单位的日期分量相同,则返回 YES
大于或等于给定单位,否则为 NO。

Starting from iOS8, this became much easier:

-(BOOL)isDate:(NSDate *)date1 onSameWeekAs:(NSDate *)date2
{
    return [[NSCalendar currentCalendar] isDate:date1
                                    equalToDate:date2
                              toUnitGranularity:NSCalendarUnitWeekOfYear];
}

From the documentation:

returns YES if both dates have equal date component for all units
greater than or equal to the given unit, otherwise NO.

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