Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 检查两个日期是否相隔一个月_Objective C_Nsdate_Nscalendar_Nsdatecomponents - Fatal编程技术网

Objective c 检查两个日期是否相隔一个月

Objective c 检查两个日期是否相隔一个月,objective-c,nsdate,nscalendar,nsdatecomponents,Objective C,Nsdate,Nscalendar,Nsdatecomponents,我在检查两个日期是否相隔一个月。我读到我应该使用这个方法组件:fromDate:toDate:options:fromNSCalendar 我的代码 #define FORMAT_DATE_TIME @"dd-MM-yyyy HH:mm" NSDate *updateDate = [Utils getDateFromString:airport.updateOn withFormat:FORMAT_DATE_TIME]; NSDate *now = [NSDate date]; NSStrin

我在检查两个日期是否相隔一个月。我读到我应该使用这个方法
组件:fromDate:toDate:options:
from
NSCalendar

我的代码

#define FORMAT_DATE_TIME @"dd-MM-yyyy HH:mm"

NSDate *updateDate = [Utils getDateFromString:airport.updateOn withFormat:FORMAT_DATE_TIME];
NSDate *now = [NSDate date];
NSString *nowString = [Utils getStringFromDate:now withFormat:FORMAT_DATE_TIME];
now = [Utils getDateFromString:nowString withFormat:FORMAT_DATE_TIME];

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = [calendar components:NSCalendarUnitMonth fromDate:updateDate toDate:now options:0];

NSLog(@"%ld", (long)[comps year]);
NSLog(@"%ld", (long)[comps month]);
NSLog(@"%ld", (long)[comps day]);
以下是日志:

(内陆开发银行)年份:9223372036854775807

(lldb)月份:0

(lldb)日:9223372036854775807

(lldb)po now 2017-08-23 11:54:00+0000

(lldb)采购订单更新日期2017-08-23 11:48:00+0000


不是所有的值都应该为零吗?

下一行不包括年(
NSCalendarUnitYear
)和日(
NSCalendarUnitDay
)组件,只包括月(
NSCalendarUnitMonth
)组件

NSDateComponents *components = [calendar components:NSCalendarUnitMonth fromDate:updateDate toDate:now options:0];
添加
NSCalendarUnitYear
NSCalendarUnitDay
以将[comps year]和[comps day]设置为0

像这样:

NSDateComponents * components = [calendar components:NSCalendarUnitMonth |NSCalendarUnitYear|NSCalendarUnitDay fromDate:updateDate toDate:now options:0];
你可以用这个

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:FORMAT_DATE_TIME];
NSDate *updateDate  = [dateFormatter dateFromString:@"24-07-1990 15:20"];

NSDate *now = [NSDate date];


NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;


NSDateComponents *comps = [calendar components:unitFlags fromDate:updateDate toDate:now options:0];


NSLog(@"%ld", (long)[comps year]);
NSLog(@"%ld", (long)[comps month]);
NSLog(@"%ld", (long)[comps day]);

我认为这是默认的“unset”值(相当于2^63-1)。如果打印
NSDateComponents
is,则可能无法打印年份和日期,因为您没有在
组件:fromDate:toDate:options:
中请求它。您不需要将日期转换为字符串或从字符串转换为日期。不,不应该。这只是未初始化的变量,其中的值是不可预测的,不应该被引用。@bruno再次检查我第一次没有很好地阅读你的问题