Ios 两个日期之间经过的天数总是相差一天
我在计算两次约会之间的天数。我是这样做的:Ios 两个日期之间经过的天数总是相差一天,ios,objective-c,nsdate,nscalendar,Ios,Objective C,Nsdate,Nscalendar,我在计算两次约会之间的天数。我是这样做的: NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; unsigned int calendarFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSDate *dateToCheck = (self.subscriptionEnd
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int calendarFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDate *dateToCheck = (self.subscriptionEnd ?: self.trialEnd);
NSLog(@"dateToCheck: %@", dateToCheck);
NSLog(@"current date: %@", [self systemTimeZoneDate]);
NSDateComponents *components = [gregorian components:calendarFlags
fromDate:[self systemTimeZoneDate]
toDate:dateToCheck
options:0];
return [components day] >= 0 ?: 0;
在撰写本文时,NSLog输出了以下内容:
2014-09-05 22:56:20.054 tweepy[9635:60b] dateToCheck: 2014-10-05 08:02:51 PM +0000
2014-09-05 22:56:20.057 tweepy[9635:60b] current date: 2014-09-05 10:56:20 PM +0000
它返回一天的差异,因为iOS认为这一天是YDM格式的
我应该在某处指明日期格式吗
以下是如何设置self.subscriptionEnd
的代码:
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.day = 30;
self.subscriptionEnd = [gregorianCalendar dateByAddingComponents:dateComponents toDate:[self systemTimeZoneDate] options:0];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.day = 2;
self.trialEnd = [gregorian dateByAddingComponents:dateComponents toDate:[self systemTimeZoneDate] options:0];
以下是如何设置self.trialEnd
的代码:
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.day = 30;
self.subscriptionEnd = [gregorianCalendar dateByAddingComponents:dateComponents toDate:[self systemTimeZoneDate] options:0];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.day = 2;
self.trialEnd = [gregorian dateByAddingComponents:dateComponents toDate:[self systemTimeZoneDate] options:0];
日期格式与此无关。所发生的是,
NSDateComponents
提供了您所要求的,以年、月、日为单位的总差异。如果只需要天数,则只需提供NSDayCalendarUnit
。报告明确指出:
有些操作可能是不明确的,并且计算行为是特定于日历的,但通常较大的组件将在较小的组件之前计算;例如,在公历中,结果可能是1个月5天,而不是0个月35天
NSDate对“格式”一无所知。如何创建存储在属性中的NSDATE?如果您正在解析字符串,那么它肯定关心格式。我在问题中添加了新代码。事实上,我更感兴趣的是找到多少天。如果您使用NSDateFormatter将字符串日期解析为NSDate,并且不指定任何格式,则使用的日期格式将是您的区域设置的“默认”格式。此外,日期格式化程序将假定日期值(除非带有时区指示符)为“本地时间”,并将其映射到生成的NSDate对象中的GMT/UTC时间。Net是指您必须非常小心地注意日期转换操作。(请记住,NSDate对象始终应包含UTC时间。)一旦将日期转换为NSDate对象,它就没有“格式”,其时区固定为UTC。当您使用NSDATEFALATATER转换为字符串形式时,您映射到所需的时区和格式。(但是注意NSCANDENAR确实有时区设置,并且在提取组件时需要考虑这一点)。我之前做过,得到了相同的结果:相差1天而不是29天。我只是使用NSDayCalendarUnit运行代码,输出如下:dateToCheck:2014-10-05 22:26:32+0000当前日期:2014-09-05 22:43:12+0000天:29Yeah此处相同。我的代码的第一个版本很好。它在其他地方返回1:)
return[components day]>=0?:0代码>返回1表示为真。我已将该行重写为return[components day]>=0?[成分日]:0代码>