我得到24小时格式的两个日期字符串如何比较?例如
当前日期:2013-06-27 13:51
服务器日期:2013-06-27 11:51
当前日期不能大于服务器日期。请帮助
这是datepicker
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小时格式,并且最终日期时间显示不正确。
答案 0 :(得分: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");
答案 1 :(得分:0)
试试这个类实用程序:
#import <Foundation/Foundation.h>
@interface NSDate (Compare)
-(BOOL) isLaterThanOrEqualTo:(NSDate*)date;
-(BOOL) isEarlierThanOrEqualTo:(NSDate*)date;
-(BOOL) isLaterThan:(NSDate*)date;
-(BOOL) isEarlierThan:(NSDate*)date;
@end
#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
答案 2 :(得分:0)
尝试使用此代码进行日期之间的任何比较...您不应该以字符串的形式比较日期。将转换前的日期与字符串进行比较。使用格式化程序的dateFromString
函数将self.serverDate转换为日期格式,方法是将日期格式指定为dateString的日期格式。然后使用以下函数比较日期。
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");
答案 3 :(得分: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);
答案 4 :(得分: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");