我正在尝试比较两个日期,无论哪个是新的或旧的,我的日期是以下格式:
2013-06-03 09:30:35
(YYYY-MM-DD HH:MM:SS)
请给我一些想法,两者目前都是字符串格式?
那么可以转换广告支票吗?
答案 0 :(得分:2)
使用NSDate
将这些字符串转换为dateFormatter
。
您可以查看此链接以下是您的问题的良好讨论
How to compare two NSDates: Which is more recent?
答案 1 :(得分:1)
试试这个
NSString到NSDate
NSString *dateString = @"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDate转换为NSString:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@", strDate);
[dateFormatter release];
请阅读本文
if ([yourFirstDate compare:secondDate] == NSOrderedDescending) {
NSLog(@"yourFirstDate is later than secondDate");
} else if ([yourFirstDate compare:secondDate] == NSOrderedAscending) {
NSLog(@"yourFirstDate is earlier than secondDate");
} else {
NSLog(@"dates are the same");
}
与@DIVYU的链接相同