dateByAddingComponents 并使用 NSDateComponents 获取日期差异

发布于 2024-08-26 15:19:01 字数 1290 浏览 12 评论 0原文

我在向日期添加值以及获取日期之间的差异时遇到问题。 计算的日期和成分不正确。

因此,对于相加,如果我添加 1.5 个月,我只会得到 1 个月,但是如果我添加任何整数,即(1 或 2 或 3 等),它会正确计算。

Float32 addAmount = 1.5;

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setMonth:addAmount];

NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
 [gregorian setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

 NSDate *newDate2 = [gregorian dateByAddingComponents:components toDate:Date1 options:0];

现在为了区别,如果我有一个日期正好添加了一年(与上面的代码几乎相同),它会正确添加,但是当计算差异时,我得到 0 年、11 个月和 30 天。

NSDate *startDate = Date1;
NSDate *endDate = Date2;

NSCalendar *gregorian = [[NSCalendar alloc]
          initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags
              fromDate:startDate
                toDate:endDate options:0];

NSInteger years = [components year];
NSInteger months = [components month];
NSInteger days = [components day];

我做错了什么?此外,我还在添加和差异函数的选项中添加了 kCFCalendarComponentsWrap 常量,但没有区别。

谢谢

I am having problems with adding values to dates and also getting differences between dates.
The dates and components calculated are incorrect.

So for adding, if I add 1.5 months, I only get 1 month, however if I add any whole number ie (1 or 2 or 3 and etc) it calculates correctly.

Float32 addAmount = 1.5;

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setMonth:addAmount];

NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
 [gregorian setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

 NSDate *newDate2 = [gregorian dateByAddingComponents:components toDate:Date1 options:0];

Now for difference, if I have a date that has been added with exactly one year (almost same code as above), it adds correctly, but when the difference is calculated, I get 0 years, 11 months and 30 days.

NSDate *startDate = Date1;
NSDate *endDate = Date2;

NSCalendar *gregorian = [[NSCalendar alloc]
          initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags
              fromDate:startDate
                toDate:endDate options:0];

NSInteger years = [components year];
NSInteger months = [components month];
NSInteger days = [components day];

What am I doing wrong? Also I have added the kCFCalendarComponentsWrap constanct in the options for both adding and difference functions but with no difference.

Thanks

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

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

发布评论

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

评论(2

深居我梦 2024-09-02 15:19:01

所以对于添加来说,如果我添加 1.5 个月,我
只有 1 个月,但是如果我添加任何
整数即(1或2或3等)
它计算正确。

NSDateComponents 中的 setMonth: 方法采用 NSInteger,而不是浮点数。所以行为是正确的,因为它只是将 1.5 截断为 1。

现在区别一下,如果我有约会的话
已经添加了一个
年(与上面的代码几乎相同),它
添加正确,但是当
计算差异,我得到0
年零 11 个月零 30 天。

除非您显示如何创建 Date1 和 Date2 变量的代码,否则没有真正的方法可以判断。

(上面也泄漏了内存;始终将分配与释放/自动释放相匹配。并且尽量不要给变量提供大写字母,因为作为风格问题,这应该只针对类名完成)

So for adding, if I add 1.5 months, I
only get 1 month, however if I add any
whole number ie (1 or 2 or 3 and etc)
it calculates correctly.

The setMonth: method in NSDateComponents takes an NSInteger, not a floating point number. So the behaviour is correct, as it's simply truncating the 1.5 to 1.

Now for difference, if I have a date
that has been added with exactly one
year (almost same code as above), it
adds correctly, but when the
difference is calculated, I get 0
years, 11 months and 30 days.

Unless you show the code for how your Date1 and Date2 variables are created, there's no real way to tell.

(You're also leaking memory above; always match an alloc with a release/autorelease. And try not to give your variables capital letters, since as a matter of style, that should only be done for class names)

滥情空心 2024-09-02 15:19:01

我终于发现了日期差异的问题,当我将其保存到数据库时,我使用了带有双精度值的 timeintervalsince1970 ,但是当填充它并将其设置为日期选择器时,我使用的是 int 列类型。

感谢毛茸茸的青蛙引导我走向正确的方向。

I finally found the problem with the date difference, when I was saving it to a db, I used timeintervalsince1970 with an double value, but when populating it and setting it to a datepicker, I was using a int column type.

Thanks Shaggy Frog for steering me in the right direction.

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