检查两个日期是否相隔一个月
Check if two dates are one month apart
我正在尝试检查两个日期是否相隔 1 个月。我读到我应该使用 NSCalendar
中的这种方法 components:fromDate:toDate:options:
我的代码
#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]);
这些是日志:
(lldb) Year: 9223372036854775807
(lldb) Month: 0
(lldb) Day: 9223372036854775807
(lldb) po now 2017-08-23 11:54:00 +0000
(lldb) po updateDate 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]);
我正在尝试检查两个日期是否相隔 1 个月。我读到我应该使用 NSCalendar
components:fromDate:toDate:options:
我的代码
#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]);
这些是日志:
(lldb) Year: 9223372036854775807
(lldb) Month: 0
(lldb) Day: 9223372036854775807
(lldb) po now 2017-08-23 11:54:00 +0000
(lldb) po updateDate 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]);