2

我得到两个 24 小时格式的日期字符串如何比较?例如

当前日期:2013-06-27 13:51
服务器日期:2013-06-27 11:51

当前日期不能大于服务器日期。请帮忙

这是日期选择器

 NSDateFormatter *datePickerFormat = [[NSDateFormatter alloc] init];
 [datePickerFormat setDateFormat:@"yyyy-MM-dd HH:mm"];
 self.myCurrentDate = [datePickerFormat stringFromDate:self.myDatePicker.date];

 NSDate *myDate = [datePickerFormat dateFromString:self.myCurrentDate];

我无法将其转换为 24 小时格式,并且最终日期时间显示不正确。

4

5 回答 5

5

尝试这个,

//  The current date :2013-06-27 13:51
//  The server date :2013-06-27 11:51

NSString *strCurrentDate = @"2013-06-27 13:51";
NSString *strServerDate =@"2013-06-27 11:51";
NSDateFormatter *datePickerFormat = [[NSDateFormatter alloc] init];
[datePickerFormat setDateFormat:@"yyyy-MM-dd HH:mm"];
NSDate *currentDate = [datePickerFormat dateFromString:strCurrentDate];
NSDate *serverDate = [datePickerFormat dateFromString:strServerDate];

NSLog(@"date %@",currentDate);
NSLog(@"date %@",serverDate);

NSComparisonResult result;

result = [currentDate compare:serverDate]; // comparing two dates

if(result == NSOrderedAscending)
    NSLog(@"current date is less");
else if(result == NSOrderedDescending)
    NSLog(@"server date is less");
else if(result == NSOrderedSame)
    NSLog(@"Both dates are same");
else
    NSLog(@"Date cannot be compared");
于 2013-06-27T05:15:58.173 回答
0

您最初无法比较两个日期字符串,您必须使用以下代码将该字符串转换为两个日期

//  The current date :2013-06-27 13:51
//  The server date :2013-06-27 11:51

    NSString *serverDate = @"2013-06-27 13:51";
        NSDateFormatter *datePickerFormat = [[NSDateFormatter alloc] init];
        [datePickerFormat setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
        [datePickerFormat setDateFormat:@"yyyy-MM-dd HH:mm"];
        NSDate *convertedDate = [datePickerFormat dateFromString:serverDate];
        NSLog(@"date %@",convertedDate);

转换后,您可以使用@prince 所说的以下代码进行比较

NSDate *today = [NSDate date]; // current date
NSDate *newDate = self.serverDate; // other date 

NSComparisonResult result; 

result = [today compare:newDate]; // comparing two dates

if(result == NSOrderedAscending)
    NSLog(@"today is less");
else if(result == NSOrderedDescending)
    NSLog(@"newDate is less");
else if(result == NSOrderedSame)
    NSLog(@"Both dates are same");
else
    NSLog(@"Date cannot be compared");
于 2013-06-27T04:51:25.940 回答
0

尝试使用此代码进行日期之间的任何比较...您不应该以字符串的形式比较日期。比较转换为字符串之前的日期。dateFromString通过将确切的日期格式指定为dateString 的格式,使用格式化程序的功能将 self.serverDate 转换为日期格式。然后使用以下函数比较日期。

NSDate *today = [NSDate date]; // current date
NSDate *newDate = self.serverDate; // other date 

NSComparisonResult result; 

result = [today compare:newDate]; // comparing two dates

if(result == NSOrderedAscending)
    NSLog(@"today is less");
else if(result == NSOrderedDescending)
    NSLog(@"newDate is less");
else if(result == NSOrderedSame)
    NSLog(@"Both dates are same");
else
    NSLog(@"Date cannot be compared");
于 2013-06-27T04:21:44.330 回答
0

试试这个类实用程序:

。H

#import <Foundation/Foundation.h>

@interface NSDate (Compare)
-(BOOL) isLaterThanOrEqualTo:(NSDate*)date;
-(BOOL) isEarlierThanOrEqualTo:(NSDate*)date;
-(BOOL) isLaterThan:(NSDate*)date;
-(BOOL) isEarlierThan:(NSDate*)date;
@end

.m

#import "NSDate+Compare.h"

@implementation NSDate (Compare)

-(BOOL) isLaterThanOrEqualTo:(NSDate*)date {
    return !([self compare:date] == NSOrderedAscending);
}
-(BOOL) isEarlierThanOrEqualTo:(NSDate*)date {
    return !([self compare:date] == NSOrderedDescending);
}
-(BOOL) isLaterThan:(NSDate*)date {
    return ([self compare:date] == NSOrderedDescending);
}
-(BOOL) isEarlierThan:(NSDate*)date {
    return ([self compare:date] == NSOrderedAscending);
}

@end
于 2013-06-27T04:08:25.977 回答
0
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
                [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                NSDate *date1 = [dateFormatter dateFromString:TodatDate];

                NSDateFormatter *dateFormatte = [[[NSDateFormatter alloc] init] autorelease];
                [dateFormatte setDateFormat:@"yyyy-MM-dd"];
                NSDate *date2 = [dateFormatte dateFromString:ServerDate];

                unsigned int unitFlags = NSDayCalendarUnit;

                NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
                NSDateComponents *comps = [gregorian components:unitFlags fromDate:date1  toDate:date2  options:0];

                int days = [comps day];
                NSLog(@"%d",days);
于 2013-06-27T04:40:57.940 回答