dateByAddingComponents 并使用 NSDateComponents 获取日期差异
我在向日期添加值以及获取日期之间的差异时遇到问题。 计算的日期和成分不正确。
因此,对于相加,如果我添加 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSDateComponents
中的setMonth:
方法采用NSInteger
,而不是浮点数。所以行为是正确的,因为它只是将 1.5 截断为 1。除非您显示如何创建 Date1 和 Date2 变量的代码,否则没有真正的方法可以判断。
(上面也泄漏了内存;始终将分配与释放/自动释放相匹配。并且尽量不要给变量提供大写字母,因为作为风格问题,这应该只针对类名完成)
The
setMonth:
method inNSDateComponents
takes anNSInteger
, not a floating point number. So the behaviour is correct, as it's simply truncating the 1.5 to 1.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)我终于发现了日期差异的问题,当我将其保存到数据库时,我使用了带有双精度值的 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.