如何获得两个日期之间的天数objective-c [duplicate]

时间:2022-04-25 15:02:38

This question already has an answer here:

这个问题已经有了答案:

I'm trying to make a label that says how many days left till event. I want to calculate the difference between todays date and the events date. I'm using this code and its giving me -4600. It works fine till I use todays date.

我在做一个标签,上面写离事件还有多少天。我想计算一下今天日期和事件日期之间的差异。我使用这个代码,得到-4600。在我用今天的日期之前,一切正常。

NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"yyyy-MM-dd"];
    NSDate *startDate = [NSDate date];
    NSDate *endDate = [f dateFromString:end];
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
                                                        fromDate:startDate
                                                          toDate:endDate
                                                         options:0];
    return components.day;
return components.day;

1 个解决方案

#1


17  

By default [NSDate date] return date in like 2013-08-06 08:50:25 +0000 and you have set your formatter to yyyy-MM-dd this, so you have to convert your startDate to yyyy-MM-dd this form..

默认情况下,[NSDate]返回日期为2013-08-06 08:50:25 +0000,您已经将格式化程序设置为yyyyyy - mm -dd,因此您必须将startDate转换为yyyyyyy - mm -dd。

Try this

试试这个

NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"yyyy-MM-ddHH:mm:ss ZZZ"];
    NSDate *startDate = [NSDate date];
    NSLog(@"%@",startDate);
    NSDate *endDate = [f dateFromString:end];
    NSLog(@"%@",endDate);


    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
                                                        fromDate:startDate
                                                          toDate:endDate
                                                         options:0];
    return components.day; 

#1


17  

By default [NSDate date] return date in like 2013-08-06 08:50:25 +0000 and you have set your formatter to yyyy-MM-dd this, so you have to convert your startDate to yyyy-MM-dd this form..

默认情况下,[NSDate]返回日期为2013-08-06 08:50:25 +0000,您已经将格式化程序设置为yyyyyy - mm -dd,因此您必须将startDate转换为yyyyyyy - mm -dd。

Try this

试试这个

NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"yyyy-MM-ddHH:mm:ss ZZZ"];
    NSDate *startDate = [NSDate date];
    NSLog(@"%@",startDate);
    NSDate *endDate = [f dateFromString:end];
    NSLog(@"%@",endDate);


    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
                                                        fromDate:startDate
                                                          toDate:endDate
                                                         options:0];
    return components.day;