简体   繁体   English

如何通过使用两个不同的日期选择器找到两个日期之间的差异

[英]How to find the difference between two dates by using two different date pickers

i did a code to find difference between two dates using two different date pickers. 我做了一个代码,使用两个不同的日期选择器来查找两个日期之间的差异。 i couldn't find the difference between two dates. 我找不到两个日期之间的差异。 the difference of two dates is always zero. 两个日期的差始终为零。

@property (strong, nonatomic) IBOutlet UILabel *date1;
@property (strong, nonatomic) IBOutlet UILabel *date2;
- (IBAction)cale1:(id)sender;
- (IBAction)cale2:(id)sender;
@property (strong, nonatomic) IBOutlet UIDatePicker *datePicker1;
@property (strong, nonatomic) IBOutlet UIDatePicker *datePicker2;
@property NSDate *d1,*d2;

@implementation ViewController

- (IBAction)cale1:(id)sender {
    NSDateFormatter *dc=[[NSDateFormatter alloc]init];
    dc.dateStyle=NSDateFormatterFullStyle;
    _d1=_datePicker1.date;
    _date1.text=[dc stringFromDate:_datePicker1.date];
    NSLog(@"Date %@",_date1);
}

- (IBAction)cale2:(id)sender {
    NSDateFormatter *dc=[[NSDateFormatter alloc]init];
    dc.dateStyle=NSDateFormatterFullStyle;
   _d2=_datePicker2.date;
    _date2.text=[dc stringFromDate:_datePicker2.date];
    NSLog(@"Date %@",_d2);

    [self isSameDay:_d1 otherDay:_d2];

}
-(BOOL)isSameDay:(NSDate *)date1 otherDay:(NSDate *)date2{
    NSCalendar *cale=[NSCalendar currentCalendar];
    unsigned unitFlags=NSYearCalendarUnit| NSMonthCalendarUnit| NSDayCalendarUnit;
    NSDateComponents *comp1=[cale components:unitFlags fromDate:date1];
    NSDateComponents *comp2=[cale components:unitFlags fromDate:date2];


   // NSString *comp=[NSString stringWithFormat:@"%d", [comp1 day]==[comp2 day]&&[comp1 month]==[comp2 month]&&[comp1 year]==[comp2 year]];
    BOOL a=[comp1 day]==[comp2 day]&&[comp1 month]==[comp2 month]&&[comp1 year]==[comp2 year];

    NSLog(@"Date Differ%hhd",a);
    return a;
}

Are the dates that are in the compare method valid dates? 比较方法中的日期是否有效?

If they are you can just do: 如果是,则可以执行以下操作:

BOOL difference = [date1 timeIntervalSinceDate:date2] > 0; 

Difference will be true if the dates are not the same. 如果日期不同,则差异是正确的。 timeIntervalSinceDate returns NSTimeInterval which is: timeIntervalSinceDate返回NSTimeInterval,它是:

Used to specify a time interval, in seconds. 用于指定时间间隔,以秒为单位。

For comparing two dates you need to use the NSDateComponents.I modified your function "isSameDay". 为了比较两个日期,您需要使用NSDateComponents。我修改了函数“ isSameDay”。 It returns the number of day differences between two dates. 它返回两个日期之间的天差数。

-(int)isSameDay:(NSDate *)date1 otherDay:(NSDate *)date2
{    
    NSDateComponents *components;

    int days;

    components = [[NSCalendar currentCalendar] components: NSDayCalendarUnit
                                             fromDate: date1 toDate: date2 options: 0];
    days = (int) [components day];

    NSLog(@"days %d",abs(days));

    return days;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM