我想比较两个日期:date1和date2
2011-06-06 12:59:48.994 Project[419:707] firstDate:2011-06-06 10:59:21 +0000
2011-06-06 12:59:49.004 Project[419:707] selectedData:2011-06-06 10:59:17 +0000
Run Code Online (Sandbox Code Playgroud)
但这些日期有不同的时间,当我使用NSOrderedSame它不能正常工作,我该如何解决?
我的代码:
NSDate *firstDate = [[appDelegate.project objectAtIndex:i]objectAtIndex:3];
NSDate *secondDate = [[appDelegate.project objectAtIndex:i]objectAtIndex:4];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger comps = (NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit);
NSDateComponents *date1Components = [calendar components:comps
fromDate:firstDate];
NSDateComponents *date2Components = [calendar components:comps
fromDate:secondDate];
NSDateComponents *date3Components = [calendar components:comps fromDate:appDelegate.selectedDate];
NSLog(@"firstDate:%@", [date1Components date]);
NSLog(@"secondDate:%@", [date2Components date]);
NSLog(@"selectedData:%@", [date3Components date]);
NSComparisonResult compareStart = [[date1Components date] compare: [date3Components date]];
NSComparisonResult compareEnd = [[date2Components date] compare: [date3Components …Run Code Online (Sandbox Code Playgroud) 我有一个NSString(例如"2011-04-12 19:23:39"),我将其格式化为NSDate的方法如下:
[inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [inputFormatter dateFromString:newDateString];
Run Code Online (Sandbox Code Playgroud)
但是当我记录日期时它输出的是:
2011-04-12 23:23:39 +0000
大约需要4个小时.我错过了什么吗?可能是时区问题?