Ios 比较两个NSDate对象是否相同
下面是我的比较代码:Ios 比较两个NSDate对象是否相同,ios,objective-c,Ios,Objective C,下面是我的比较代码: int countTrue = 0; //NSDate *datee = [[self.reminderResultsArray objectAtIndex:0] date]; for(int i = 0; i < self.reminderResultsArray.count; i++){ if([[NSCalendar currentCalendar] isDate:self.actualSelectedDate inSameDayAsDate:[[
int countTrue = 0;
//NSDate *datee = [[self.reminderResultsArray objectAtIndex:0] date];
for(int i = 0; i < self.reminderResultsArray.count; i++){
if([[NSCalendar currentCalendar] isDate:self.actualSelectedDate inSameDayAsDate:[[self.reminderResultsArray objectAtIndex:i] date]]){
countTrue++;
NSLog(@"Count true: %d", countTrue);
}
}
右边的
countTrue
值应该是3,但它只返回值1。我的代码有问题吗如果你观察日志,那么时间是不同的,因为NSCALDENER方法将把它们视为不同的日期。
如果只想进行日期比较,请首先使用日期格式化程序dd/mm/yyyy创建日期对象,然后比较日期
或者,您可以检查日、月和年组件
+ (BOOL)isSameDayWithDate1:(NSDate*)date1 date2:(NSDate*)date2 {
NSCalendar* calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
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];
}
如果您观察日志,则时间不同,因此NSCALPEND方法将把它们视为不同的日期。 如果只想进行日期比较,请首先使用日期格式化程序dd/mm/yyyy创建日期对象,然后比较日期
或者,您可以检查日、月和年组件+ (BOOL)isSameDayWithDate1:(NSDate*)date1 date2:(NSDate*)date2 {
NSCalendar* calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
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];
}
如果您只关心
NSDate
对象的日期部分,您可以这样做:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let actualSelectedDateString = "2017-06-02 16:00:00 +0000"
let actualSelectedDate = dateFormatter.dateFromString(actualSelectedDateString)!
let dateStrings = ["2017-06-02 09:38:35 +0000",
"2017-05-27 09:37:48 +0000",
"2017-06-02 09:38:35 +0000",
"2017-06-02 10:09:48 +0000",
"2017-06-02 10:12:30 +0000",
"2017-06-03 08:49:33 +0000"]
let dates = dateStrings.map({dateFormatter.dateFromString($0)!})
let calendar = NSCalendar.currentCalendar()
let count = dates.filter({calendar.isDate($0, inSameDayAsDate: actualSelectedDate)}).count
如果您只关心
NSDate
对象的日期部分,您可以这样做:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let actualSelectedDateString = "2017-06-02 16:00:00 +0000"
let actualSelectedDate = dateFormatter.dateFromString(actualSelectedDateString)!
let dateStrings = ["2017-06-02 09:38:35 +0000",
"2017-05-27 09:37:48 +0000",
"2017-06-02 09:38:35 +0000",
"2017-06-02 10:09:48 +0000",
"2017-06-02 10:12:30 +0000",
"2017-06-03 08:49:33 +0000"]
let dates = dateStrings.map({dateFormatter.dateFromString($0)!})
let calendar = NSCalendar.currentCalendar()
let count = dates.filter({calendar.isDate($0, inSameDayAsDate: actualSelectedDate)}).count
试试这个
int countTrue = 0 ;
NSString *actualSelectedDateAsString = @"2017-06-02 16:00:00 +0000";
NSArray *reminderResultsArray = @[@"2017-06-02 09:38:35 +0000",
@"2017-05-27 09:37:48 +0000",
@"2017-06-02 09:38:35 +0000",
@"2017-06-02 10:09:48 +0000",
@ "2017-06-02 10:12:30 +0000",
@"2017-06-03 08:49:33 +0000"];
// use date formatter to convert string to date and set the date format as per your requirement.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate *actualSelectedDate = [dateFormatter dateFromString:actualSelectedDateAsString]; // convert string to date.
for (int i=0 ; i<reminderResultsArray.count ; i++){
if( [[NSCalendar currentCalendar]isDate:actualSelectedDate inSameDayAsDate:[dateFormatter dateFromString:[reminderResultsArray objectAtIndex:i]]]){
countTrue++;
NSLog(@"Count true: %d", countTrue);
}
}
NSLog(@"Count true: %d", countTrue); // result would be 4
int countTrue=0;
NSString*actualSelectedDateAsString=@“2017-06-02 16:00:00+0000”;
NSArray*提醒错误结果报告=@[@“2017-06-02 09:38:35+0000”,
@"2017-05-27 09:37:48 +0000",
@"2017-06-02 09:38:35 +0000",
@"2017-06-02 10:09:48 +0000",
@ "2017-06-02 10:12:30 +0000",
@"2017-06-03 08:49:33 +0000"];
//使用日期格式化程序将字符串转换为日期,并根据需要设置日期格式。
NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init];
[日期格式化程序setDateFormat:@“yyyy-MM-dd HH:MM:ss Z”];
NSDate*actualSelectedDate=[dateFormatter dateFromString:actualSelectedDateAsString];//将字符串转换为日期。
对于(int i=0;i试试这个
int countTrue = 0 ;
NSString *actualSelectedDateAsString = @"2017-06-02 16:00:00 +0000";
NSArray *reminderResultsArray = @[@"2017-06-02 09:38:35 +0000",
@"2017-05-27 09:37:48 +0000",
@"2017-06-02 09:38:35 +0000",
@"2017-06-02 10:09:48 +0000",
@ "2017-06-02 10:12:30 +0000",
@"2017-06-03 08:49:33 +0000"];
// use date formatter to convert string to date and set the date format as per your requirement.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate *actualSelectedDate = [dateFormatter dateFromString:actualSelectedDateAsString]; // convert string to date.
for (int i=0 ; i<reminderResultsArray.count ; i++){
if( [[NSCalendar currentCalendar]isDate:actualSelectedDate inSameDayAsDate:[dateFormatter dateFromString:[reminderResultsArray objectAtIndex:i]]]){
countTrue++;
NSLog(@"Count true: %d", countTrue);
}
}
NSLog(@"Count true: %d", countTrue); // result would be 4
int countTrue=0;
NSString*actualSelectedDateAsString=@“2017-06-02 16:00:00+0000”;
NSArray*提醒错误结果报告=@[@“2017-06-02 09:38:35+0000”,
@"2017-05-27 09:37:48 +0000",
@"2017-06-02 09:38:35 +0000",
@"2017-06-02 10:09:48 +0000",
@ "2017-06-02 10:12:30 +0000",
@"2017-06-03 08:49:33 +0000"];
//使用日期格式化程序将字符串转换为日期,并根据需要设置日期格式。
NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init];
[日期格式化程序setDateFormat:@“yyyy-MM-dd HH:MM:ss Z”];
NSDate*actualSelectedDate=[dateFormatter dateFromString:actualSelectedDateAsString];//将字符串转换为日期。
对于(int i=0;i记录的日期是相对于UTC(即时区+0000)的描述。这可能会误导您
您从[NSCalendar currentCalendar]
获取的日历对象将使用系统的当前时区。除非您的时区是UTC,否则这意味着输出中显示的月份不是与-isDate:inSameDayAsDate:
比较相关的月份。(在一般情况下,根据时区的不同,日期的字符串表示形式可能会有所不同,而不仅仅是月份的某一天。)
要想弄清楚为什么比较不能按您认为的方式工作,您首先需要弄清楚时区中所有这些日期的年、月和月日。然后就会清楚了。我希望-isDate:inSameDayAsDate:
方法完全按照它应该的方式工作,而您只是误解了您使用的日期的实际日期(在您的时区内)
或者,如果您真的对UTC中的日期感兴趣,那么您需要使用一个日历对象,该对象的时区设置为[NSTimeZone timeZoneWithName:@“UTC”]
当记录日期时,它描述的是相对于UTC的日期(即时区+0000)。这可能会误导您
您从[NSCalendar currentCalendar]
获取的日历对象将使用系统的当前时区。除非您的时区是UTC,否则这意味着输出中显示的月份不是与-isDate:inSameDayAsDate:
比较相关的月份。(在一般情况下,根据时区的不同,日期的字符串表示形式可能会有所不同,而不仅仅是月份的某一天。)
要想弄清楚为什么比较不能按您认为的方式工作,您首先需要弄清楚时区中所有这些日期的年、月和月日。然后就会清楚了。我希望-isDate:inSameDayAsDate:
方法完全按照它应该的方式工作,而您只是误解了您使用的日期的实际日期(在您的时区内)
或者,如果您真的对UTC中的日期感兴趣,则需要使用其时区设置为[NSTimeZone timeZoneWithName:@“UTC”的日历对象
你只关心日期部分而想忽略时间部分吗?@AndréSlotta只关心日期部分而想忽略时间部分吗?@AndréSlotta只关心日期部分方法在samedayasdate
中应该检查两个日期是否在同一天内。根据文档,是的应该检查两个日期是否在同一天。我已经用另一种方法更新了答案,看看是否有用。方法inSameDayAsDate
应该检查两个日期是否在同一天内。根据文档,是的,它应该检查两个日期是否在同一天。我已经用另一种方法更新了答案乙酰胆碱