日期无法比较

时间:2014-01-03 05:56:26

标签: ios objective-c nsdate nsdateformatter

我想检查用户选择的日期是否与当前日期相同。我正在使用此代码。我可以检查所选日期是在过去还是将来,但我无法检测它是否相同,因为当我记录两个日期时,它是这种格式。我的意思是今天的日期时间和所选的日期时间之间存在差异。所以它被认为是更少。这是日志:

  

所选日期:2014-01-03 05:53:53 +0000,今日日期:2014-01-03 05:54:20 +0000

这是我的代码:

 NSDate *today = [NSDate date]; // it will give you current date
NSDate *newDate = datePicker.date; // your date

NSComparisonResult result;
//  has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending
NSLog(@"selected date:%@ , today date: %@",newDate,today);
result = [today compare:newDate];
NSLog(@"result %d",result);
if(result==NSOrderedAscending)
{
    NSLog(@"big ascending");
    [AJNotificationView showNoticeInView:[[UIApplication sharedApplication] delegate].window
                                    type:AJNotificationTypeRed
                                   title:@"Birthdate should be less than today date."
                         linedBackground:AJLinedBackgroundTypeDisabled
                               hideAfter:GZAJNotificationDelay];
    return;

}
else if(result==NSOrderedDescending){
    NSLog(@"fine");
    [UIView animateWithDuration:0.25 delay:0.0 options: UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         // self.view.frame=CGRectMake(0, -30, 320, 200);
                         subview.frame=CGRectMake(0, 640, 320, 180);
                         //                             CGRect f1;
                         //                             f1=self.view.frame;
                         //                             f1.origin.y=f1.origin.y-100;
                         // self.view.frame=f1;
                         // v1.backgroundColor=[UIColor blackColor];
                     }
                     completion:^(BOOL finished){
                         if(finished)  {NSLog(@"Finished end !!!!!");}
                     }];
}

else{
    NSLog(@"same");
    [AJNotificationView showNoticeInView:[[UIApplication sharedApplication] delegate].window
                                    type:AJNotificationTypeRed
                                   title:@"Birthdate should be less than today date."
                         linedBackground:AJLinedBackgroundTypeDisabled
                               hideAfter:GZAJNotificationDelay];
    return;
}

4 个答案:

答案 0 :(得分:1)

试试这个 -

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat: @"MM-dd-yyyy"];
NSDate *date1 = [dateFormat dateFromString:firstDate];
NSDate *date2 = [dateFormat dateFromString:secondDate];

NSComparisonResult comparisonResult = [date1 compare:date2];

if(comparisonResult==NSOrderedAscending)
{
}
else if(comparisonResult ==NSOrderedDescending)
{
}
else
{
    // dates are same
}

答案 1 :(得分:1)

- (BOOL)isSameDay:(NSDate *)date1 withDate2:(NSDate *)date2
{
    if (nil == date1 || nil == date2) {
        return NO;
    }

    NSCalendar *calendar = [NSCalendar currentCalendar];

    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
    NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];

    return [comp1 day] == [comp2 day] &&
    [comp1 month] == [comp2 month] &&
    [comp1 year]  == [comp2 year];
}

使用此方法比较两个日期是否在同一天。

答案 2 :(得分:0)

您需要使用以下方法转换秒数。毫秒:

  long time1=[[NSDate date] timeIntervalSince1970];
  long time2=[newDate timeIntervalSince1970];

之后你可以比较正常的长变量。这是一个很好的做法。

您也可以使用

if ([[NSDate date] isEqualToDate:startTime.date]) {
NSLog(@"currentDate is equal to startTime"); 
}

答案 3 :(得分:-1)

请与NSDateFormatter约会,并从日期中删除“时间”。 这是日期类:

     NSDateFormatter *format = [[NSDateFormatter alloc] init];
     [format setDateFormat:@"MM/dd/yyyy"];
相关问题